{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div style=\"text-align: right\" align=\"right\"><i>Peter Norvig<br>December 1–25, 2021</i></div>\n",
    "\n",
    "# Advent of Code 2021\n",
    "\n",
    "I'm doing [Advent of Code](https://adventofcode.com/2021) (AoC) this year. I'm not competing for points, just participating for fun.\n",
    "\n",
    "To fully understand each puzzle's instructions, click on the link (e.g. [**Day 1**](https://adventofcode.com/2021/day/1)); I give only brief summaries here. \n",
    "\n",
    "Part of the idea of AoC is that you have to make some design choices to solve Part 1 *before* you get to see the instructions for Part 2. So there is a tension of wanting the solution to Part 1 to provide components that can be re-used in Part 2, without falling victim to [YAGNI](https://en.wikipedia.org/wiki/You_aren%27t_gonna_need_it). In this notebook I won't refactor the code for Part 1 after I see what is requested in Part 2 (although I may edit the code for clarity, without changing the initial approach). Sometimes I will explore further, inventing my own \"Part 3\".\n",
    "\n",
    "This year's AoC theme involves Santa's Elves on a submarine. Gary J. Grady ([@GaryJGrady](https://twitter.com/GaryJGrady/) on Twitter) has some nice drawings to set the scene:\n",
    "\n",
    "<img src=\"https://pbs.twimg.com/media/FFiywKpWYAAm6B5?format=jpg&name=medium\" width=400>\n",
    "\n",
    "# Day 0: Preparations\n",
    "\n",
    "I put some imports and functions that I thought would be useful in a notebook of utility functions, [AdventUtils.ipynb](AdventUtils.ipynb)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [],
   "source": [
    "%run AdventUtils.ipynb\n",
    "\n",
    "current_year = 2021"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# [Day 1](https://adventofcode.com/2021/day/1): Sonar Sweep\n",
    "\n",
    "\n",
    "- **Input**: Each item in the input is an integer depth measurement.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "────────────────────────────────────────────────────────────────────────────────────────────────────\n",
      "Puzzle input ➜ 2000 lines:\n",
      "────────────────────────────────────────────────────────────────────────────────────────────────────\n",
      "148\n",
      "167\n",
      "168\n",
      "169\n",
      "182\n",
      "188\n",
      "...\n",
      "────────────────────────────────────────────────────────────────────────────────────────────────────\n",
      "Parsed representation ➜ 2000 ints:\n",
      "────────────────────────────────────────────────────────────────────────────────────────────────────\n",
      "148\n",
      "167\n",
      "168\n",
      "169\n",
      "182\n",
      "188\n",
      "...\n"
     ]
    }
   ],
   "source": [
    "in1 = parse(1, int)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- **Part 1**: **How many measurements are larger than the previous measurement?**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.000 seconds for correct answer: 1,400\n"
     ]
    }
   ],
   "source": [
    "def increases(measurements: Sequence[int]) -> int:\n",
    "    \"\"\"How many measurements are larger than the previous measurement?\"\"\"\n",
    "    return quantify(measurements[i] > measurements[i - 1] \n",
    "                    for i in range(1, len(measurements)))\n",
    "\n",
    "answer(1.1, 1400, lambda: increases(in1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- **Part 2**: Consider sums of a three-measurement sliding window. **How many sums are larger than the previous sum?**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.001 seconds for correct answer: 1,429\n"
     ]
    }
   ],
   "source": [
    "def windows(sequence, width) -> List[Sequence]:\n",
    "    \"\"\"All sliding (overlapping) windows of given `width` in sequence.\"\"\"\n",
    "    return [sequence[i:i+width] \n",
    "            for i in range(len(sequence) + 1 - width)]\n",
    "\n",
    "answer(1.2, 1429, lambda: increases(mapt(sum, windows(in1, 3))))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- **Part 3**: Visualization\n",
    "\n",
    "Let's take a look at where the depths are taking us:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYsAAAD4CAYAAAAdIcpQAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjMsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+AADFEAAAgAElEQVR4nO3de5TcZZ3n8fc3FwJIIBcayJIMiRo9ZHZmkWkTCtDxRgKsmuA4LnOSJWeXsxUE56DoyQXniJddsHtHh4OLkDg4A5sozowKOKt2Z1FBoO2muUNaSMRk0ksgDQESEHLr7/7xPEUq3XXrTv1+denP65w6VfX8nqr69q8v335+z83cHRERkVLG1ToAERGpf0oWIiJSlpKFiIiUpWQhIiJlKVmIiEhZE2odQBJOPPFEnz17dq3DEBFpKA899NCL7t5S6FhTJovZs2fT29tb6zBERBqKmW0rdqxhLkOZ2flm9rSZbTGz1bWOR0RkLGmIZGFm44EbgQuAecBfmdm82kYlIjJ2NESyAOYDW9z9WXffB9wOLK5xTCIiY0ajJItTge15z/tj2VvMLGtmvWbWOzAwkGpwIiLNrlGShRUoO2xRK3df5+6t7t7a0lKwM19EREapUZJFPzAr7/lM4LkaxSIiMuY0SrJ4EJhrZnPM7CjgYuCuJD6oqwuuuy7ci4hI0BDzLNz9gJl9BugAxgPfdfenqv05XV1w7rkwOAjjxsF990EmU+1PERFpPI3SssDdf+ru73L3d7j7/0jiMy6/PCQKCPfnngurViXxSSIijaVhkkUa+voOfz44CO3tsGBBbeIREakXShZ5pk4tXN7TA8uWpRuLiEg9UbLI85WvFD+2YQPMmwcXXRTux42Do4/WZSoRGRuULPJks7BwYfHjfX1wxx3h3h327g2XqcaNg+nTYd269GIVEUmTksUQHR2wciVMGME4MXfYtQtWrFDCEJHmZO5evlaDaW1t9WosUX700aH1MBITJ8K+fUf80SIiq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zcnv5pqapBuvA51m9pCZZWPZye6+A8IPM3BSDeLKuZjDf4lrfb5g5OenFuftvxL+A82ZY2aPmNk9Zva+WHZqjCWNuEbyfUv7fL0PeMHdN+eVpX6+hvxtSPVnTMnikELXFVMfV2xmxwE/BD7r7ruBm4B3AGcAOwhNYUg33nPc/UzgAuAKM3t/ibqpnkczOwr4OPDPsagezlcpxeJI+7x9ETgAbIhFO4A/cvf3AFcB3zOz41OMa6Tft7S/n3/F4f+QpH6+CvxtKFq1SAxHFJuSxSH9wKy85zOB59IMwMwmEn4YNrj7j+SCPSEAAAH1SURBVADc/QV3P+jug8B3OHTpJLV43f25eL8T+HGM4YXc5aV4vzPtuKILgIfd/YUYY83PVzTS85NafLFj86PA0niphHiZ56X4+CFCf8C7Ylz5l6oSiWsU37c0z9cE4BPAD/LiTfV8FfrbQMo/Y0oWhzwIzDWzOfG/1YuBu9L68HhN9Bagz92/mVeef73/IiA3UuMu4GIzm2Rmc4C5hI61asf1NjObnHtM6CB9Mn5+bjTFcuDOvLguiSMyzgJezTWVE3LYf3y1Pl95Rnp+OoCFZjY1XoJZGMuqyszOB1YBH3f3P+SVt5jZ+Pj47YTz82yMbY+ZnRV/Ri/J+1qqGddIv29p/r5+BPitu791eSnN81XsbwNp/4wdSS99s90IowieIfyX8MWUP/tcQpPwceDReLsQ+N/AE7H8LmBG3mu+GGN9miMccVEirrcTRpo8BjyVOy/AdOBuYHO8nxbLDbgxxvUE0JrgOTsWeAk4Ia8s9fNFSFY7gP2E/94uHc35IfQhbIm3/5JQXFsI161zP2M3x7p/Eb+/jwEPAx/Le59Wwh/v3wH/i7jyQ5XjGvH3rdq/r4XiiuX/CFw2pG6a56vY34ZUf8a03IeIiJSly1AiIlKWkoWIiJSlZCEiImUpWYiISFlKFiIiUpaShYiIlKVkISIiZf1/3ctmnxkATOgAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.plot(in1, 'b.'); plt.ylabel('Depth'); plt.gca().invert_yaxis();"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It looks like Gary Grady was right; the submarine is descending at a steep angle."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# [Day 2](https://adventofcode.com/2021/day/2): Dive! \n",
    "\n",
    "- **Input**: Each item in the input is a command name (\"forward\", \"down\", or \"up\") followed by an integer.\n",
    "\n",
    "I'll parse a command into a tuple like `('forward', 2)`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "────────────────────────────────────────────────────────────────────────────────────────────────────\n",
      "Puzzle input ➜ 1000 lines:\n",
      "────────────────────────────────────────────────────────────────────────────────────────────────────\n",
      "forward 2\n",
      "down 7\n",
      "down 8\n",
      "forward 9\n",
      "down 8\n",
      "forward 9\n",
      "...\n",
      "────────────────────────────────────────────────────────────────────────────────────────────────────\n",
      "Parsed representation ➜ 1000 tuples:\n",
      "────────────────────────────────────────────────────────────────────────────────────────────────────\n",
      "('forward', 2)\n",
      "('down', 7)\n",
      "('down', 8)\n",
      "('forward', 9)\n",
      "('down', 8)\n",
      "('forward', 9)\n",
      "...\n"
     ]
    }
   ],
   "source": [
    "in2 = parse(2, atoms)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- **Part 1**: Calculate the horizontal position and depth you would have after following the planned course. **What do you get if you multiply your final horizontal position by your final depth?**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.000 seconds for correct answer: 1,670,340\n"
     ]
    }
   ],
   "source": [
    "def drive(commands) -> int:\n",
    "    \"\"\"What is the product of position and depth after following commands?\"\"\"\n",
    "    pos = depth = 0\n",
    "    for (op, n) in commands:\n",
    "        if op == 'forward': pos += n\n",
    "        if op == 'down':    depth += n\n",
    "        if op == 'up':      depth -= n\n",
    "    return pos * depth\n",
    "\n",
    "answer(2.1, 1_670_340, lambda: drive(in2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- **Part 2**: Using the new interpretation of the commands, calculate the horizontal position and depth you would have after following the planned course. **What do you get if you multiply your final horizontal position by your final depth?**\n",
    "\n",
    "The *new interpretation* is that the \"down\" and \"up\" commands no longer change depth, rather they change *aim*, and going forward *n* units both increments position by *n* and depth by *aim* × *n*."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.000 seconds for correct answer: 1,954,293,920\n"
     ]
    }
   ],
   "source": [
    "def drive2(commands) -> int:\n",
    "    \"\"\"What is the product of position and depth after following commands?\n",
    "    This time we have to keep track of `aim` as well.\"\"\"\n",
    "    pos = depth = aim = 0\n",
    "    for (op, n) in commands:\n",
    "        if op == 'forward': pos += n; depth += aim * n\n",
    "        if op == 'down':    aim += n\n",
    "        if op == 'up':      aim -= n\n",
    "    return pos * depth\n",
    "\n",
    "answer(2.2, 1_954_293_920, lambda: drive2(in2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# [Day 3](https://adventofcode.com/2021/day/3): Binary Diagnostic\n",
    "\n",
    "- **Input**: Each item in the input is a bit string of `0`s and `1`s.\n",
    "\n",
    "I'll parse them as strings; I won't convert them into ints."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "────────────────────────────────────────────────────────────────────────────────────────────────────\n",
      "Puzzle input ➜ 1000 lines:\n",
      "────────────────────────────────────────────────────────────────────────────────────────────────────\n",
      "101000111100\n",
      "000011111101\n",
      "011100000100\n",
      "100100010000\n",
      "011110010100\n",
      "101001100000\n",
      "...\n"
     ]
    }
   ],
   "source": [
    "in3 = parse(3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- **Part 1**: Use the binary numbers in your diagnostic report to calculate the gamma rate and epsilon rate, then multiply them together. **What is the power consumption of the submarine?**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.001 seconds for correct answer: 2,261,546\n"
     ]
    }
   ],
   "source": [
    "def common(strs, i) -> Char: # '1' or '0'\n",
    "    \"\"\"The bit that is most common in position i among strs.\"\"\"\n",
    "    bits = [s[i] for s in strs]\n",
    "    return '1' if bits.count('1') >= bits.count('0') else '0'\n",
    "\n",
    "def uncommon(strs, i) -> Char: # '1' or '0'\n",
    "    \"\"\"The bit that is least common in position i among strs.\"\"\"\n",
    "    return '1' if common(strs, i) == '0' else '0'\n",
    "\n",
    "def epsilon(strs) -> str:\n",
    "    \"\"\"The bit string formed from most common bit at each position.\"\"\"\n",
    "    return cat(common(strs, i) for i in range(len(strs[0])))\n",
    "\n",
    "def gamma(strs) -> str:\n",
    "    \"\"\"The bit string formed from most uncommon bit at each position.\"\"\"\n",
    "    return cat(uncommon(strs, i) for i in range(len(strs[0])))\n",
    "\n",
    "def power(strs) -> int: \n",
    "    \"\"\"Product of epsilon and gamma rates.\"\"\"\n",
    "    return int(epsilon(strs), 2) * int(gamma(strs), 2)\n",
    "    \n",
    "answer(3.1, 2261546, lambda: power(in3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- **Part 2**: Use the binary numbers in your diagnostic report to calculate the oxygen generator rating and CO2 scrubber rating, then multiply them together. **What is the life support rating of the submarine?**\n",
    "\n",
    "This time I'll have a single function, `select_str` which selects the str that survives the process of picking strs with the most common or uncommon bit at each position. Then I call `select_str` with `common` to get the oxygen rating and `uncommon` to get the CO2 rating."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.000 seconds for correct answer: 6,775,520\n"
     ]
    }
   ],
   "source": [
    "def select_str(strs, common_fn, i=0) -> str:\n",
    "    \"\"\"Select a str from strs according to common_fn:\n",
    "    Going left-to-right, repeatedly select just the strs that have the right i-th bit.\n",
    "    When only one string is remains, return it.\"\"\"\n",
    "    if len(strs) == 1:\n",
    "        return strs[0]\n",
    "    else:\n",
    "        bit = common_fn(strs, i)\n",
    "        selected = [s for s in strs if s[i] == bit]\n",
    "        return select_str(selected, common_fn, i + 1)\n",
    "\n",
    "def life_support(strs) -> int: \n",
    "    \"\"\"The product of oxygen (most common select) and CO2 (least common select) rates.\"\"\"\n",
    "    return int(select_str(strs, common), 2) * int(select_str(strs, uncommon), 2)\n",
    "    \n",
    "answer(3.2, 6775520, lambda: life_support(in3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# [Day 4](https://adventofcode.com/2021/day/4): Giant Squid\n",
    "\n",
    "- **Input**: The first item of the input is a permutation of the integers 0-99. Subsequent items are bingo boards: 5 lines of 5 ints each. items are separated by *two* newlines. \n",
    "\n",
    "I'll represent a board as a tuple of 25 ints; that makes `parse` easy: the permutation of integers and the bingo boards can both be parsed by `ints`. (Bingo games will be played against a giant squid; we get to choose which board we want to play.)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "────────────────────────────────────────────────────────────────────────────────────────────────────\n",
      "Puzzle input ➜ 601 lines:\n",
      "────────────────────────────────────────────────────────────────────────────────────────────────────\n",
      "73,42,95,35,13,40,99,92,33,30,83,1,36,93,59,90,55,25,77,44,37,62,41,47,80,23,51,61,21,20,76,8,71 ...\n",
      "\n",
      "91  5 64 81 34\n",
      "15 99 31 63 65\n",
      "45 39 54 93 83\n",
      "51 14 23 86 32\n",
      "...\n",
      "────────────────────────────────────────────────────────────────────────────────────────────────────\n",
      "Parsed representation ➜ 101 tuples:\n",
      "────────────────────────────────────────────────────────────────────────────────────────────────────\n",
      "(73, 42, 95, 35, 13, 40, 99, 92, 33, 30, 83, 1, 36, 93, 59, 90, 55, 25, 77, 44, 37, 62, 41, 47,  ...\n",
      "(91, 5, 64, 81, 34, 15, 99, 31, 63, 65, 45, 39, 54, 93, 83, 51, 14, 23, 86, 32, 19, 22, 16, 13, 3)\n",
      "(20, 83, 38, 85, 70, 69, 12, 14, 26, 84, 19, 76, 45, 78, 99, 22, 80, 90, 33, 46, 75, 31, 21, 6, 28)\n",
      "(22, 52, 65, 75, 2, 91, 12, 45, 18, 94, 38, 66, 85, 39, 1, 24, 36, 55, 74, 3, 89, 14, 79, 99, 48)\n",
      "(19, 58, 95, 22, 6, 48, 28, 57, 30, 72, 12, 67, 15, 37, 18, 33, 1, 49, 90, 60, 35, 41, 47, 11, 84)\n",
      "(89, 27, 65, 68, 19, 38, 83, 21, 81, 91, 67, 61, 87, 30, 10, 36, 45, 66, 56, 4, 82, 71, 44, 96, 90)\n",
      "...\n"
     ]
    }
   ],
   "source": [
    "order, *boards = in4 = parse(4, ints, sep='\\n\\n')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- **Part 1**: To guarantee victory against the giant squid, figure out which board will win first. **What will your final score be if you choose that board?**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "I'm worried about an ambiguity: what if two boards win at the same time? I'll have to assume Eric arranged it so that can't happen. I'll define `bingo_winners` to return a list of boards that win when a number has just been called, and I'll arbitrarily choose the first of them."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.004 seconds for correct answer: 39,902\n"
     ]
    }
   ],
   "source": [
    "B     = 5 # Bingo board is size B by B.\n",
    "Board = Tuple[int] # B * B ints\n",
    "Line  = List[int]  # B ints\n",
    "\n",
    "def lines(square) -> Tuple[Line, Line]:\n",
    "    \"\"\"The two lines (horizontal and vertical) through square number `square`.\"\"\"\n",
    "    def sq(x, y) -> int: return x + B * y\n",
    "    return ([sq(x, square // B) for x in range(B)], \n",
    "            [sq(square % B, y)  for y in range(B)])\n",
    "\n",
    "def bingo_winners(boards, drawn, just_called) -> List[Board]:\n",
    "    \"\"\"Board(s) that win due to the number just called.\"\"\"\n",
    "    def filled(board, line) -> bool: return all(board[n] in drawn for n in line)\n",
    "    return [board for board in boards\n",
    "            if just_called in board\n",
    "            and any(filled(board, line) \n",
    "                    for line in lines(board.index(just_called)))]\n",
    "\n",
    "def bingo_score(board, drawn, just_called) -> int:\n",
    "    \"\"\"Sum of unmarked numbers multiplied by the number just called.\"\"\"\n",
    "    unmarked = sum(n for n in board if n not in drawn)\n",
    "    return unmarked * just_called\n",
    "\n",
    "def bingo(boards, order) -> int: \n",
    "    \"\"\"What is the final score of the first winning board?\"\"\"\n",
    "    drawn = set()\n",
    "    for num in order:\n",
    "        drawn.add(num)\n",
    "        winners = bingo_winners(boards, drawn, num)\n",
    "        if winners:\n",
    "            return bingo_score(winners[0], drawn, num)\n",
    "\n",
    "answer(4.1, 39902, lambda: bingo(boards, order))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- **Part 2**: Figure out which board will win last. **Once it wins, what would its final score be?**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.007 seconds for correct answer: 26,936\n"
     ]
    }
   ],
   "source": [
    "def bingo_last(boards, order) -> int: \n",
    "    \"\"\"What is the final score of the last winning board?\"\"\"\n",
    "    remaining_boards = set(boards)\n",
    "    drawn = set()\n",
    "    for num in order:\n",
    "        drawn.add(num)\n",
    "        winners = bingo_winners(remaining_boards, drawn, num)\n",
    "        remaining_boards -= set(winners)\n",
    "        if not remaining_boards:\n",
    "            return bingo_score(winners[-1], drawn, num)\n",
    "                \n",
    "answer(4.2, 26936, lambda: bingo_last(boards, order))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"https://pbs.twimg.com/media/FFykNslXMAwe_xA?format=jpg&name=medium\" width=400 title=\"Drawing by Gary Grady @GaryJGrady\">"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# [Day 5](https://adventofcode.com/2021/day/5): Hydrothermal Venture\n",
    "\n",
    "- **Input**: Each item in the input is a \"line\" denoted by start and end x,y points, e.g. \"`0,9 -> 5,9`\". \n",
    "\n",
    "I'll represent a line as a 4-tuple of integers, e.g. `(0, 9, 5, 9)`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "────────────────────────────────────────────────────────────────────────────────────────────────────\n",
      "Puzzle input ➜ 500 lines:\n",
      "────────────────────────────────────────────────────────────────────────────────────────────────────\n",
      "409,872 -> 409,963\n",
      "149,412 -> 281,280\n",
      "435,281 -> 435,362\n",
      "52,208 -> 969,208\n",
      "427,265 -> 884,265\n",
      "779,741 -> 779,738\n",
      "...\n",
      "────────────────────────────────────────────────────────────────────────────────────────────────────\n",
      "Parsed representation ➜ 500 tuples:\n",
      "────────────────────────────────────────────────────────────────────────────────────────────────────\n",
      "(409, 872, 409, 963)\n",
      "(149, 412, 281, 280)\n",
      "(435, 281, 435, 362)\n",
      "(52, 208, 969, 208)\n",
      "(427, 265, 884, 265)\n",
      "(779, 741, 779, 738)\n",
      "...\n"
     ]
    }
   ],
   "source": [
    "in5 = parse(5, ints)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- **Part 1**: Consider only horizontal and vertical lines. **At how many points do at least two lines overlap?**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.041 seconds for correct answer: 7,436\n"
     ]
    }
   ],
   "source": [
    "def points(line) -> List[Point]:\n",
    "    \"\"\"All the (integer) points on a line.\"\"\"\n",
    "    x1, y1, x2, y2 = line\n",
    "    if x1 == x2:\n",
    "        return [(x1, y) for y in cover(y1, y2)]\n",
    "    elif y1 == y2:\n",
    "        return [(x, y1) for x in cover(x1, x2)]\n",
    "    else: # non-orthogonal lines not allowed\n",
    "        return []\n",
    "    \n",
    "def overlaps(lines) -> int:\n",
    "    \"\"\"How many points overlap 2 or more lines?\"\"\"\n",
    "    counts = Counter(flatten(map(points, lines)))\n",
    "    return quantify(counts[p] >= 2 for p in counts)\n",
    "\n",
    "answer(5.1, 7436, lambda: overlaps(in5))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- **Part 2**: Consider all of the lines (including diagonals, which are all at ±45°). **At how many points do at least two lines overlap?**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For Part 2 I'll redefine `points` and `overlaps` in a way that doesn't break Part 1:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.051 seconds for correct answer: 7,436\n",
      "0.076 seconds for correct answer: 21,104\n"
     ]
    }
   ],
   "source": [
    "def points(line, diagonal=False) -> bool:\n",
    "    \"\"\"All the (integer) points on a line; optionally allow diagonal lines.\"\"\"\n",
    "    x1, y1, x2, y2 = line\n",
    "    if diagonal or x1 == x2 or y1 == y2:\n",
    "        dx, dy = sign(x2 - x1), sign(y2 - y1)\n",
    "        length = max(abs(x2 - x1), abs(y2 - y1))\n",
    "        return [(x1 + k * dx, y1 + k * dy) for k in range(length + 1)]\n",
    "    else: # non-orthogonal lines not allowed when diagonal is False\n",
    "        return []\n",
    "    \n",
    "def overlaps(lines, diagonal=False) -> int:\n",
    "    \"\"\"How many points overlap 2 or more lines?\"\"\"\n",
    "    counts = Counter(flatten(points(line, diagonal) for line in lines))\n",
    "    return quantify(counts[p] >= 2 for p in counts)\n",
    "\n",
    "assert points((1, 1, 1, 3), False) == [(1, 1), (1, 2), (1, 3)]\n",
    "assert points((1, 1, 3, 3), False) == []\n",
    "assert points((1, 1, 3, 3), True) == [(1, 1), (2, 2), (3, 3)]\n",
    "assert points((9, 7, 7, 9), True) == [(9, 7), (8, 8), (7, 9)]\n",
    "\n",
    "answer(5.1,  7436, lambda: overlaps(in5, diagonal=False)) # Make sure it still works\n",
    "answer(5.2, 21104, lambda: overlaps(in5, diagonal=True))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# [Day 6](https://adventofcode.com/2021/day/6): Lanternfish\n",
    "\n",
    "- **Input**: The input is comma-separated integers, each the age of a lanternfish (according to its internal timer). \n",
    "\n",
    "Over time, the lanternfish age and reproduce: Each day, their timer decrements by one. The day after it reaches 0 it is reset to 6 and a new lanternfish is born with an internal timer of 8."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "────────────────────────────────────────────────────────────────────────────────────────────────────\n",
      "Puzzle input ➜ 1 line:\n",
      "────────────────────────────────────────────────────────────────────────────────────────────────────\n",
      "5,4,3,5,1,1,2,1,2,1,3,2,3,4,5,1,2,4,3,2,5,1,4,2,1,1,2,5,4,4,4,1,5,4,5,2,1,2,5,5,4,1,3,1,4,2,4,2, ...\n",
      "────────────────────────────────────────────────────────────────────────────────────────────────────\n",
      "Parsed representation ➜ 300 ints:\n",
      "────────────────────────────────────────────────────────────────────────────────────────────────────\n",
      "5\n",
      "4\n",
      "3\n",
      "5\n",
      "1\n",
      "1\n",
      "...\n"
     ]
    }
   ],
   "source": [
    "in6 = parse(6, int, sep=',')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- **Part 1**: Find a way to simulate lanternfish. **How many lanternfish would there be after 80 days?**\n",
    "\n",
    "Although the puzzle instructions treats each fish individually, I won't take the bait (pun intended). \n",
    "\n",
    "Instead, I'll use a `Counter` of fish, and process all the fish of each age group together, all at once. That way each update will be *O*(9) = *O*(1), not *O*(*n*). I have a hunch that Part 2 will involve a ton-o'-fish."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.000 seconds for correct answer: 350,917\n"
     ]
    }
   ],
   "source": [
    "Fish = Counter # Represent a school of fish as a Counter of their internal timers\n",
    "\n",
    "def simulate(fish, days=1) -> Fish:\n",
    "    \"\"\"Simulate the aging and birth of fish over `days`.\"\"\"\n",
    "    for day in range(days):\n",
    "        fish = Fish({t - 1: fish[t] for t in fish})\n",
    "        if -1 in fish: # births\n",
    "            fish[6] += fish[-1]\n",
    "            fish[8] = fish[-1]\n",
    "            del fish[-1]\n",
    "    return fish\n",
    "        \n",
    "assert simulate(Fish((3, 4, 3, 1, 2))) == Fish((2, 3, 2, 0, 1))\n",
    "assert simulate(Fish((2, 3, 2, 0, 1))) == Fish((1, 2, 1, 6, 0, 8))\n",
    "assert Fish((1, 1, 1, 6, 8, 6)) == {1: 3, 6: 2, 8: 1}\n",
    "\n",
    "answer(6.1, 350917, lambda: total(simulate(Fish(in6), 80)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- **Part 2**: **How many lanternfish would there be after 256 days?**\n",
    "\n",
    "My hunch was right, so part 2 is straightforward:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.001 seconds for correct answer: 1,592,918,715,629\n"
     ]
    }
   ],
   "source": [
    "answer(6.2, 1_592_918_715_629, lambda: total(simulate(Fish(in6), 256)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "That's over a trillion lanternfish. Latest [estimates](https://www.google.com/search?q=how+many+fish+are+in+the+sea) say that there are in fact trillions of fish in the sea. But not trillions of lanternfish, and not increasing from 300 to over a trillion in just 256 days.\n",
    "\n",
    "<img src=\"https://pbs.twimg.com/media/FF9GGGJXEAEQ0MD?format=jpg&name=medium\" width=400 title=\"Drawing by Gary Grady @GaryJGrady\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# [Day 7](https://adventofcode.com/2021/day/7): The Treachery of Whales\n",
    "\n",
    "- **Input**: The input is a single line of comma-separated integers, each  the horizontal position of a crab (in its own submarine)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "────────────────────────────────────────────────────────────────────────────────────────────────────\n",
      "Puzzle input ➜ 1 line:\n",
      "────────────────────────────────────────────────────────────────────────────────────────────────────\n",
      "1101,1,29,67,1102,0,1,65,1008,65,35,66,1005,66,28,1,67,65,20,4,0,1001,65,1,65,1106,0,8,99,35,67, ...\n",
      "────────────────────────────────────────────────────────────────────────────────────────────────────\n",
      "Parsed representation ➜ 1000 ints:\n",
      "────────────────────────────────────────────────────────────────────────────────────────────────────\n",
      "1101\n",
      "1\n",
      "29\n",
      "67\n",
      "1102\n",
      "0\n",
      "...\n"
     ]
    }
   ],
   "source": [
    "in7 = parse(7, int, sep=',')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The idea is that if you can get the crabs to all align in one horizontal position, they can save you from a giant whale by opening up an escape route to a cave.\n",
    "\n",
    "<img src=\"https://pbs.twimg.com/media/FGCMbMhXMA8zAJD?format=jpg&name=medium\" width=400 title=\"@GaryJGrady\">\n",
    "\n",
    "- **Part 1**: Determine the horizontal position that the crabs can align to using the least fuel possible. (Each unit of horizontal travel costs one unit of fuel.) **How much fuel must they spend to align to that position?**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.000 seconds for correct answer: 352,707\n"
     ]
    }
   ],
   "source": [
    "def fuel_cost(positions) -> int:\n",
    "    \"\"\"How much fuel does it cost to get everyone to the best alignment point?\"\"\"\n",
    "    # I happen to know that the best alignment point is the median\n",
    "    align = int(median(positions))\n",
    "    return sum(abs(p - align) for p in positions)\n",
    "\n",
    "answer(7.1, 352707, lambda: fuel_cost(in7))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- **Part 2**: Determine the horizontal position that the crabs can align to using the least fuel possible so they can make you an escape route! (Now for each crab the first unit of travel costs 1, the second 2, the third 3, and so on.)  **How much fuel must they spend to align to that position?**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.361 seconds for correct answer: 95,519,693\n"
     ]
    }
   ],
   "source": [
    "def fuel_cost2(positions) -> int:\n",
    "    \"\"\"How much fuel does it cost to get everyone to the best alignment point, \n",
    "    with nonlinear fuel costs?\"\"\"\n",
    "    # I don't know the best alignment point, so I'll try all of them\n",
    "    return min(sum(burn_rate2(p, align) for p in positions)\n",
    "               for align in cover(*positions))\n",
    "\n",
    "def burn_rate2(p, align) -> int:\n",
    "    \"\"\"The first step costs 1, the second 2, etc. (i.e. triangular numbers).\"\"\"\n",
    "    steps = abs(p - align)\n",
    "    return steps * (steps + 1) // 2\n",
    "\n",
    "answer(7.2, 95519693, lambda: fuel_cost2(in7))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- **Part 3**: Analysis and Visualization\n",
    "\n",
    "Now that I got the right answer and have some time to think about it, if the travel cost were exactly quadratic, we would be minimizing the sum of square distances, and Legendre and Gauss knew that the **mean**, not the **median**, is the alignment point that does that. What's the mean of the positions?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "490.543"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "positions = in7\n",
    "mean(positions)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "That's not an integer, but I'll try it, along with the integers above and below it:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{490: 95519693, 491: 95519725, 490.543: 95519083.0}"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "{align: sum(burn_rate2(p, align) for p in positions)\n",
    " for align in [490, 491, mean(positions)]}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We see that rounding down gives the right answer, rounding up does a bit worse, and using the exact mean  gives a total fuel cost that is *better* than the correct answer (but is apparently not a legal alignment point). A reddit user with the name CrashAndSideburns looked more carefully into the use of the mean, and wrote [a paper](https://www.reddit.com/r/adventofcode/comments/rawxad/2021_day_7_part_2_i_wrote_a_paper_on_todays/) showing that the best alignment point must be within ±0.5 from the mean.\n",
    "\n",
    "Below I show a histogram of the number of crabs at each range of horizontal positions, along with red stars for the two alignment points (median and mean)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[376.0, 490.543]"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYUAAAEGCAYAAACKB4k+AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjMsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+AADFEAAAYVklEQVR4nO3deZhldX3n8fdHQAEVASm0ZbHRIWaIK+koGdcJRlkU1KgBGWWQTMcJGpc42mhGjEweIUYNJg5OR4hNhgGVoBBxY3gEYiagzQ4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1KQJHVMClKPJPe3I1dem+SzSbafwz4+mWSfdvo9Gyz7f/MVqzQIdkmVeiT5aVU9qp0+Hbisem4u3JT9SZsDawrS9P4F+HcASd7R1h6uTfK2tuyRSc5LclVb/odt+YVJliQ5AdiurXmc3i77afueNM8tuLYdP39y2xe125+V5pkGp7d3t0pDsfWoA5DGUTtmzYHAl5P8NnAU8ByaO48vTXIRzR3fd1TVwe02j+ndR1UtS/LmqnrmFId4Fc1gbs8AdgG+meTidtmzgN+iGffmX4HnAl+f5x9RmpI1BemhtkvzhKyVwO0041M9D/hcVf2sqn4KnE0zHPI1wIuTnJjk+VX1k1kc53nAGdUM6rYWuAj4nXbZN6pqdTWDvV1J81AWaSisKUgP9YsNv9lP13xTVTe2tYiDgA8m+WpVfaDP48zUJHRvz/T9+HeqIbKmIG3cxcArkmzfjhb7SuBfkjwB+HlV/W/gr2kex7ihX7VDpE+1zz9MslWSCZrHOQ5ztFZpSn4DkTaiqi5P8ike/Kf9yaq6Is1D4T+U5AGa0TH/6xSbLweuTnJ5VR3RU/45mmfxXkUzWu67qurOJL85sB9E6oNdUiVJHZuPJEkdk4IkqWNSkCR1TAqSpI5JQZLUMSlIkjomBUlS5/8DhQ1UmTVemRMAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "stars = [median(in7), mean(in7)]\n",
    "plt.hist(in7, bins=33, rwidth=0.8); \n",
    "plt.plot(stars, [50, 50], 'r*')\n",
    "plt.ylabel('Number of Crabs'); plt.xlabel('Position')\n",
    "stars"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# [Day 8](https://adventofcode.com/2021/day/8): Seven Segment Search\n",
    "\n",
    "- **Input**: Each item in the input consists of 10 *patterns* followed by a \"`|`\", followed by 4 *output values*.\n",
    "      \n",
    "I'll split on the `|` and then extract atoms from both sides:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "────────────────────────────────────────────────────────────────────────────────────────────────────\n",
      "Puzzle input ➜ 200 lines:\n",
      "────────────────────────────────────────────────────────────────────────────────────────────────────\n",
      "daegb gadbcf cgefda edcfagb dfg acefbd fdgab fg bdcfa fcgb | cdfgba fgbc dbfac gfadbc\n",
      "bdfc dcbegf bf egfbcda gebad cfgaed bfe edfgc aegfcb gebdf | fb fb bcdfaeg fcgdeb\n",
      "cebdgaf bfcd gceab bf bfcea gceafd ecdfa fegdab bfcade fba | dfcb dagfbe fbaged bfa\n",
      "efabcg aegcdb fgaed fac dgafbc becf eadcgbf aegfc fc cagbe | ecgfa agdef eagfc gdceab\n",
      "fcdae cdeabf fga gf gabfde cgadb gadebfc cgfe aegcdf afgcd | fbgadce gadefb fag bafegd\n",
      "gecadbf bgc dacgf gaecbf cbeda dbfg bgdca bg bafcgd gdacef | cdgfa fceabg dgfb dgabc\n",
      "...\n",
      "────────────────────────────────────────────────────────────────────────────────────────────────────\n",
      "Parsed representation ➜ 200 tuples:\n",
      "────────────────────────────────────────────────────────────────────────────────────────────────────\n",
      "(('daegb', 'gadbcf', 'cgefda', 'edcfagb', 'dfg', 'acefbd', 'fdgab', 'fg', 'bdcfa', 'fcgb'), ('cd ...\n",
      "(('bdfc', 'dcbegf', 'bf', 'egfbcda', 'gebad', 'cfgaed', 'bfe', 'edfgc', 'aegfcb', 'gebdf'), ('fb ...\n",
      "(('cebdgaf', 'bfcd', 'gceab', 'bf', 'bfcea', 'gceafd', 'ecdfa', 'fegdab', 'bfcade', 'fba'), ('df ...\n",
      "(('efabcg', 'aegcdb', 'fgaed', 'fac', 'dgafbc', 'becf', 'eadcgbf', 'aegfc', 'fc', 'cagbe'), ('ec ...\n",
      "(('fcdae', 'cdeabf', 'fga', 'gf', 'gabfde', 'cgadb', 'gadebfc', 'cgfe', 'aegcdf', 'afgcd'), ('fb ...\n",
      "(('gecadbf', 'bgc', 'dacgf', 'gaecbf', 'cbeda', 'dbfg', 'bgdca', 'bg', 'bafcgd', 'gdacef'), ('cd ...\n",
      "...\n"
     ]
    }
   ],
   "source": [
    "in8 = parse(8, lambda line: mapt(atoms, line.split('|')))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "assert in8[0] == (('daegb', 'gadbcf', 'cgefda', 'edcfagb', 'dfg', 'acefbd', 'fdgab', 'fg', 'bdcfa', 'fcgb'), \n",
    "                  ('cdfgba', 'fgbc', 'dbfac', 'gfadbc'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Each pattern and output value represents a digit on a [7-segment display](https://en.wikipedia.org/wiki/Seven-segment_display), with each letter a–g representing one of the 7 segments that is turned on in that digit. The mapping of letters to segments differs for each input item, but is consistent across all the digits within each item. Here's one mapping:\n",
    "\n",
    "      aaaa\n",
    "     b    c\n",
    "     b    c\n",
    "      dddd\n",
    "     e    f\n",
    "     e    f\n",
    "      gggg\n",
    "      \n",
    "<img src=\"https://pbs.twimg.com/media/FGHtjKiWYAQGrBX?format=jpg&name=medium\" width=400 title=\"@GaryJGrady\">\n",
    "\n",
    "- **Part 1**: **In the output values, how many times do digits 1, 4, 7, or 8 appear?**\n",
    "\n",
    "That's the same as asking *how many output values have a length of 2, 4, 3, or 7 segments?*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.000 seconds for correct answer: 493\n"
     ]
    }
   ],
   "source": [
    "def lengths2437(data):\n",
    "    \"\"\"Count the output values with lengths 2, 4, 3, 7.\"\"\"\n",
    "    return quantify(len(value) in (2, 4, 3, 7) \n",
    "                    for (lhs, rhs) in data \n",
    "                    for value in rhs)\n",
    "\n",
    "answer(8.1, 493, lambda: lengths2437(in8))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- **Part 2**: For each entry, determine all of the wire/segment connections and decode the four-digit output values. **What do you get if you add up all of the output values?**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Part 2 is *tricky*. The first output value `'cdfgba'` has 6 segments, so it could be either a 0, 6, or 9. To figure out which one it is I could do some fancy constraint satisfaction. That sounds hard. Or I could exhaustively try all permutations of the ways the 7 letters can map to the 7 segments. That sounds easy!  Here's my plan:\n",
    "- Make a list of the 7! = 5,040 possible `str.maketrans` translators that permute `'abcdefg'`.\n",
    "- Decode an entry by trying all translators and keeping the one that maps all of the ten lhs patterns to a valid digit. `decode` then applies the translator to the four rhs values, concatenates them, and  converts the result into an `int`.\n",
    "  - Note that `get_digit` must *sort* the translated letters to get a key that can be looked up in `segment_map`.\n",
    "- Finally, sum up the decoding of each entry."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.599 seconds for correct answer: 1,010,460\n"
     ]
    }
   ],
   "source": [
    "segments7    = 'abcdefg'\n",
    "segment_map  = {'abcefg': '0', 'cf':     '1', 'acdeg': '2', 'acdfg':   '3', 'bcdf':   '4',\n",
    "                'abdfg':  '5', 'abdefg': '6', 'acf':   '7', 'abcdefg': '8', 'abcdfg': '9'}\n",
    "\n",
    "translators = [str.maketrans(segments7, cat(p)) for p in permutations(segments7)]\n",
    "\n",
    "def get_digit(pattern, translator) -> Optional[Char]:\n",
    "    \"\"\"Translate the pattern, and return a digit '0' to '9' if valid.\"\"\"\n",
    "    return segment_map.get(cat(sorted(pattern.translate(translator))))\n",
    "\n",
    "def decode(entry) -> int:\n",
    "    \"\"\"Decode an entry's rhs into a 4-digit integer.\"\"\"\n",
    "    lhs, rhs = entry\n",
    "    for t in translators:\n",
    "        if all(get_digit(pattern, t) for pattern in lhs):\n",
    "            return int(cat(get_digit(pattern, t) for pattern in rhs))\n",
    "\n",
    "answer(8.2, 1010460, lambda: sum(map(decode, in8)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# [Day 9](https://adventofcode.com/2021/day/9): Smoke Basin\n",
    "\n",
    "- **Input:** The input is a *heightmap*: a 2D array of characters '0'–'9' representing the heights on the ocean floor. \n",
    "\n",
    "I'll use `parse` to get a tuple of rows (where each row is a tuple of digits), and turn that into a `Grid`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "────────────────────────────────────────────────────────────────────────────────────────────────────\n",
      "Puzzle input ➜ 100 lines:\n",
      "────────────────────────────────────────────────────────────────────────────────────────────────────\n",
      "9897656789865467895698765469899988672134598894345689864101378965457932349943210987654789653198789434\n",
      "8789542499996878954329984398789976561012987789245678953212567892345791998899329899765678969997668912\n",
      "7678943978987989965998993297649875432129876567956789864487678991056899877778939769886789998766457899\n",
      "4578999868998996899867894976532986543299876476897899987569899989167898766567898654998898998655345678\n",
      "2456987657679535679756799988643498657987654345789978899789998878998919954349997543219967987543237889\n",
      "1234896545568986798645678999754989767898765456998769759899987765789329863238898659301256798793156891\n",
      "...\n",
      "────────────────────────────────────────────────────────────────────────────────────────────────────\n",
      "Parsed representation ➜ 100 tuples:\n",
      "────────────────────────────────────────────────────────────────────────────────────────────────────\n",
      "(9, 8, 9, 7, 6, 5, 6, 7, 8, 9, 8, 6, 5, 4, 6, 7, 8, 9, 5, 6, 9, 8, 7, 6, 5, 4, 6, 9, 8, 9, 9, 9, ...\n",
      "(8, 7, 8, 9, 5, 4, 2, 4, 9, 9, 9, 9, 6, 8, 7, 8, 9, 5, 4, 3, 2, 9, 9, 8, 4, 3, 9, 8, 7, 8, 9, 9, ...\n",
      "(7, 6, 7, 8, 9, 4, 3, 9, 7, 8, 9, 8, 7, 9, 8, 9, 9, 6, 5, 9, 9, 8, 9, 9, 3, 2, 9, 7, 6, 4, 9, 8, ...\n",
      "(4, 5, 7, 8, 9, 9, 9, 8, 6, 8, 9, 9, 8, 9, 9, 6, 8, 9, 9, 8, 6, 7, 8, 9, 4, 9, 7, 6, 5, 3, 2, 9, ...\n",
      "(2, 4, 5, 6, 9, 8, 7, 6, 5, 7, 6, 7, 9, 5, 3, 5, 6, 7, 9, 7, 5, 6, 7, 9, 9, 9, 8, 8, 6, 4, 3, 4, ...\n",
      "(1, 2, 3, 4, 8, 9, 6, 5, 4, 5, 5, 6, 8, 9, 8, 6, 7, 9, 8, 6, 4, 5, 6, 7, 8, 9, 9, 9, 7, 5, 4, 9, ...\n",
      "...\n"
     ]
    }
   ],
   "source": [
    "in9 = Grid(parse(9, digits))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- **Part 1**: Find all of the low points on your heightmap. **What is the sum of the risk levels of all low points on your heightmap?**\n",
    "\n",
    "A low point is a point where all the neighbors are higher. The risk level is 1 more than the height of the low point."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.015 seconds for correct answer: 607\n"
     ]
    }
   ],
   "source": [
    "def low_points(grid) -> List[Point]:\n",
    "    \"\"\"All low points on grid.\"\"\"\n",
    "    return [p for p in grid \n",
    "            if all(grid[p] < grid[nbr] for nbr in grid.neighbors(p))]\n",
    "\n",
    "def total_risk(grid) -> int:\n",
    "    \"\"\"Sum of height + 1 for all low points on grid.\"\"\"\n",
    "    return sum(grid[p] + 1 for p in low_points(grid))\n",
    "\n",
    "answer(9.1, 607, lambda: total_risk(in9))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- **Part 2**: **What do you get if you multiply together the sizes of the three largest basins?**\n",
    "    \n",
    "I thought there was an ambiguity in the definition of *basin*: what happens if there is a high point that is not of height 9, but has low points on either side of it? Wouldn't that high point then be part of two basins? The puzzle instructions says *Locations of height 9 do not count as being in any basin, and all other locations will always be part of exactly one basin.* I decided this must mean that the heightmap is carefully arranged so that every basin has only one low point and is surrounded by either edges or height 9 locations.\n",
    "\n",
    "With that assumption, I can associate each location with its low point using a [flood fill](https://en.wikipedia.org/wiki/Flood_fill) that starts from each low point. I can then get the sizes of the three largest (most common) basins and multiply them together."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.032 seconds for correct answer: 900,864\n"
     ]
    }
   ],
   "source": [
    "def find_basins(grid) -> Dict[Point, Point]:\n",
    "    \"\"\"Compute `basins` as a dict of {point: low_point_of_point's_basin} for each point in grid.\"\"\"\n",
    "    basins = {} # A dict mapping each non-9 location to its low point.\n",
    "    def flood_fill(p, low):\n",
    "        \"\"\"Spread from p in all directions until hitting a 9;\n",
    "        mark each point p as being part of the basin with `low` point.\"\"\"\n",
    "        if grid[p] < 9 and p not in basins:\n",
    "            basins[p] = low\n",
    "            for p2 in grid.neighbors(p):\n",
    "                flood_fill(p2, low)\n",
    "    for p in low_points(grid):\n",
    "        flood_fill(p, low=p)\n",
    "    return basins\n",
    "\n",
    "answer(9.2, 900864, lambda: \n",
    "       prod(count for low_point, count in \n",
    "            Counter(find_basins(in9).values()).most_common(3)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- **Part 3**: Visualization\n",
    "\n",
    "I'll make a plot  to display height 9 locations in yellow and height 0 locations in deep purple, with a gradient in between:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAi0AAAIuCAYAAABzfTjcAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjMsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+AADFEAAAgAElEQVR4nO2dT6il93nfn1xrTEZiroyQzIghZjQGGSyHaa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Oh8PhcDjFlcv14yl0+++h6eyNfe6EXZ193p1mdsez924/6ZzZPWeu7Hvt3rdGb/Hvz/+3ynzRHv7Tv67cE3bi4rFt9dC+H/WrdW4875dPfXbbPW329qWTW+ouM3vxz85tuUdeMtsbsLf1vL3WZ8POQ+33PfnM8/vWr7UPPv8vv1OZx+zJ956o3Beb79Ha93//V7+9vc4Ptd/3vtP7j1FrX7WO+d63i27Rep5K6+d97PM/2eda59Xu/W/uc3v/17vF7944uc89evf+8P2PfufSPnflT/5wa/nOb+n7Rd1XLz/85S33yEtmn/74u/te60Fdl9a51tr3rXPolZ3r+9wPjmnP+96LD24tf8rax1fdjtb5/JVH/7Z65Reb29G6Zj75uR/vc+98aXv97jSz5z7x1S33+JX2Odl6Xus8aG2beg613uO1t27fty6t4/Fbd35la/nadf18ad2bWvfT1r348hunttwS50HrfVv76tHbn9xyL7y/7y0PXJfWOrfua+qxrO9X953ef6/KhqYFh8PhcDic6mzGLQpNCwAAAAwBH1oAAABgCAhxcTgcDofDKa5crh9PgYm4AAAAMAT89xAAAAAMAR9aAAAAYAj40AIAAABDwIcWAAAAGAI+tAAAAMAQ8KEFAAAAhoAPLQAAADAEfGgBAACAIeBDCwAAAAwBH1oAAABgCPjQAgAAAEPAhxYAAAAYAj60AAAAwBDwoQUAAACGgA8tAAAAMAR8aAEAAIAh4EMLAAAADAEfWgAAAGAI+NACAAAAQ8CHFgAAABgCPrQAAADAEPChBQAAAIaADy0AAAAwBHxoAQAAgCHgQwsAAAAMAR9aAAAAYAj40AIAAABDwIcWAAAAGIL/B0Jn8qb/giH/AAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 720x720 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "def show_heights(in9, low=None):\n",
    "    plt.figure(figsize=(10, 10))\n",
    "    C = [in9[p] for p in in9]\n",
    "    plt.scatter(*T(in9), marker='s', s=10, c=C, cmap=plt.get_cmap('plasma'))\n",
    "    if low: plt.plot(*T(low_points(in9)), low, markersize=4)\n",
    "    plt.axis('square'); plt.axis('off')\n",
    "    \n",
    "show_heights(in9)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can optionally display the low points. Here I'll display them as red diamonds:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAi0AAAIuCAYAAABzfTjcAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjMsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+AADFEAAAgAElEQVR4nOy9cXAc1YHu+50eaTwzrZEsSxBh8bxxkpJ9kwdkr70hBVupW2wtvg8b6qWWxdRCvQCuwO7L5QakemCbusuyt7DNVsneLXZvhVC2c7cgZWdJ8QoC7+HUUvv2ERVwxbLAbp5RbUKWwqCsZcvyqGfksWb6/aEeuafVYx3pnDndPfP9/lH6m5H69OnTrQ7+6Rvhui4IIYQQQuKOFfUACCGEEEJk4EMLIYQQQhIBH1oIIYQQkgj40EIIIYSQRMCHFkIIIYQkAj60EEIIISQR8KGFEEIIIYmADy2EEEIISQR8aCGEEEJIIuBDCyGEEEISAR9aCCGEEJII+NBCCCGEkETAhxZCCCGEJAI+tBBCCCEkEfChhRBCCCGJgA8thBBCCEkEfGghhBBCSCLgQwshhBBCEgEfWgghhBCSCPjQQgghhJBEwIcWQgghhCQCPrQQQgghJBHwoYUQQgghiYAPLYQQQghJBHxoIYQQQkgi4EMLIYQQQhIBH1oIIYQQkgj40EIIIYSQRMCHFkLI8ggxBCFehhBDWt5HWoOozne77ZcsIlzXjWTHH975n84DyPuiwht/twW+rLBr8t7u17++3/++wk1v7uk+u/f2umzdvhe6Z8pfqMt60r/srv6PTXWZ9Vsfds//4Ma6rOOen3UHxlLYdO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WOIMb1rsMVbvKbRJj4u3H7Q6KEKSDBtnk4mt8/bBZ78AhITTek+h6/uN4o3+I21joUDdTrZE3DhlQlQu3Pyw69+41PNromZjqD8e42PGLIGZycZZZvoyW+dtpX97FcBoPV8Q/g3LhZsftrJe5nvW5iDmAnVwO/y6EbLXiBs3stQQS4gpeF0lE1vnLU7rJU5jiSG8aSGEEEJIIqDTQgghhJBEwJsWQgghhCQC3rQQQgghJBHwpoUQQgghiYA3LYQQQghJBLxpIYQQQkgi4E0LIYQQQhIBb1oIIYQQkgh400IIIYSQRMCbFkIIIYQkAt60EEIIISQR8KaFEEIIIYmANy2EEEIISQS8aSGEEEJIIuBNCyGEEEISAW9aCCGEEJIIeNNCCCGEkETAmxZCCCGEJALetBBCCCEkEfCmhRBCCCGJgDcthBBCCEkEvGkhhBBCSCLgTQshhBBCEgFvWgghhBCSCHjTQgghhJBEwJsWQgghhCQC3rQQQgghJBH8f5XDVb2zfQy1AAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 720x720 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "show_heights(in9, low='rD')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Apropos to *Smoke* and *Water,* and to the color scheme of my plot, Gary Grady's drawing for the day references  [Deep Purple](https://www.youtube.com/watch?v=_zO6lWfvM0g):\n",
    "\n",
    "<img src=\"https://pbs.twimg.com/media/FGL7YhbX0BAHDDt?format=jpg&name=medium\" title=\"@GaryJGrady\" width=400>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# [Day 10](https://adventofcode.com/2021/day/10): Syntax Scoring\n",
    "\n",
    "- **Input**: Each item in the input is a string of opening and closing brackets: `[({<` and `>})]`.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "────────────────────────────────────────────────────────────────────────────────────────────────────\n",
      "Puzzle input ➜ 102 lines:\n",
      "────────────────────────────────────────────────────────────────────────────────────────────────────\n",
      "[(([{<{(<{{[({{}{}}{[]()})<{{}()}>]}}(([{{{}[]}[[]()]}[<{}[]]{()()}]](({{}{}}{{}()}))){[{({}())[[\n",
      "<(({[<([{({[{{<>()}}[{<>()}({}{})]]<{<()<>>{[]()}}(((){}>[[][]])>}([{<[]{}>(<>[])}]))<[[[[[][]\n",
      "(<<(<{{{{<<<[(()<>){()<>}][[()()]]>{<{[]{}}<<>()>>}>{(<{<>}([]{})><(<>())<(){}>>)<(([]{})(()())) ...\n",
      "[[[[<[{[(<{{{({}<>)((){})}((()())[()()])}}><[([((){})]<[()[]]{{}<>}>)[[{[]<>}][([]{})[{}()]]]]>)<{(<\n",
      "[<(<[[((<{((<<<>[]>><<<>{}>>){<[{}<>][<>[]]><<<>()>[(){}]>})[<{[{}<>][(){}]}<[[]<>][{}[]])>{([<>[]][\n",
      "(([[[[<([[{([{<>()}{()<>}][((){})]){[{[]<>}({}<>)][(<><>)[()[]]]}}<{{({}{}){[]{}}}<{<><>}({}{})>}>\n",
      "...\n"
     ]
    }
   ],
   "source": [
    "in10 = parse(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Ideally, the brackets are balanced, but items might be *corrupted* (an extra closing bracket of the wrong kind appears in the wrong place) or *incomplete* (one or more closing brackets are missing from the end).\n",
    "  \n",
    "- **Part 1**: Find the first illegal character in each corrupted line of the navigation subsystem. **What is the total syntax error score for those errors?**\n",
    "\n",
    "The instructions for Part 1 say *Some of the lines aren't corrupted, just incomplete; you can ignore these lines for now.* That suggests we will not ignore the incomplete lines in Part 2. So I'll define `analyze_syntax` to return a tuple of two values: an error score for use in Part 1, and the missing characters for an incomplete line, which I suspect will be used in Part 2."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.001 seconds for correct answer: 367,059\n"
     ]
    }
   ],
   "source": [
    "error_scores = {')': 3, ']': 57, '}': 1197, '>': 25137}\n",
    "open_close   = {'(': ')', '[': ']', '{': '}', '<': '>'}\n",
    "\n",
    "def analyze_syntax(line) -> Tuple[int, str]:\n",
    "    \"\"\"A tuple of (error_score, missing_chars) for this line.\"\"\"\n",
    "    stack = [''] # A stack of closing characters we are looking for.\n",
    "    for c in line:\n",
    "        if c == stack[-1]:     # A correctly matched closing bracket\n",
    "            stack.pop()\n",
    "        elif c in open_close:  # A new opening bracket\n",
    "            stack.append(open_close[c])\n",
    "        else:                  # An erroneous closing bracket\n",
    "            return error_scores[c], cat(reversed(stack))\n",
    "    return 0, cat(reversed(stack))\n",
    "        \n",
    "answer(10.1, 367059, lambda: sum(analyze_syntax(line)[0] for line in in10))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- **Part 2**: Find the completion string for each incomplete line, score the completion strings, and sort the scores. **What is the middle score?**\n",
    "\n",
    "I was right; Part 2 uses the missing characters (now called a *completion string*). To score the completion string, we  treat it as a base-5 number, as shown in `completion_score`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.001 seconds for correct answer: 1,952,146,692\n"
     ]
    }
   ],
   "source": [
    "def completion_score(completion:str) -> int:\n",
    "    \"\"\"The completion score for the completion string (the missing characters).\"\"\"\n",
    "    score = completion.translate(str.maketrans(')]}>', '1234'))\n",
    "    return int(score, base=5)\n",
    "\n",
    "def median_completion_score(lines) -> int:\n",
    "    \"\"\"The median completion score out of all the uncorrupted lines.\"\"\"\n",
    "    scores = (completion_score(completion) \n",
    "              for e, completion in map(analyze_syntax, lines) \n",
    "              if e == 0)\n",
    "    return median(scores)\n",
    "\n",
    "answer(10.2, 1_952_146_692, lambda: median_completion_score(in10))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# [Day 11](https://adventofcode.com/2021/day/11): Dumbo Octopus\n",
    "\n",
    "- **Input**: The input is a 2D array of characters `0`–`9` representing the energy levels of bioluminescent [dumbo octopuses](https://www.youtube.com/watch?v=eih-VSaS2g0)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "────────────────────────────────────────────────────────────────────────────────────────────────────\n",
      "Puzzle input ➜ 10 lines:\n",
      "────────────────────────────────────────────────────────────────────────────────────────────────────\n",
      "1224346384\n",
      "5621128587\n",
      "6388426546\n",
      "1556247756\n",
      "1451811573\n",
      "1832388122\n",
      "...\n",
      "────────────────────────────────────────────────────────────────────────────────────────────────────\n",
      "Parsed representation ➜ 10 tuples:\n",
      "────────────────────────────────────────────────────────────────────────────────────────────────────\n",
      "(1, 2, 2, 4, 3, 4, 6, 3, 8, 4)\n",
      "(5, 6, 2, 1, 1, 2, 8, 5, 8, 7)\n",
      "(6, 3, 8, 8, 4, 2, 6, 5, 4, 6)\n",
      "(1, 5, 5, 6, 2, 4, 7, 7, 5, 6)\n",
      "(1, 4, 5, 1, 8, 1, 1, 5, 7, 3)\n",
      "(1, 8, 3, 2, 3, 8, 8, 1, 2, 2)\n",
      "...\n"
     ]
    }
   ],
   "source": [
    "in11 = Grid(parse(11, digits), directions8)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- **Part 1**: Given the starting energy levels of the dumbo octopuses in your cavern, simulate 100 steps. **How many total flashes are there after 100 steps?**\n",
    "\n",
    "On each step, each octopus increases by one energy unit; then the ones with an energy level over 9 emit a flash, which makes their neighbors get one more energy unit (potentially causing others to flash); then the flashers reset to zero energy."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.008 seconds for correct answer: 1,591\n"
     ]
    }
   ],
   "source": [
    "def simulate_flashes(grid, steps=100) -> int:\n",
    "    \"\"\"Simulate octopus flashes for `steps` steps and return total number of flashes.\"\"\"\n",
    "    grid = grid.copy() # Don't mutate the original grid\n",
    "    flashes = 0\n",
    "    for step in range(steps):\n",
    "        flashers = set()\n",
    "        for p in grid:\n",
    "            grid[p] += 1\n",
    "        for p in grid:\n",
    "            check_flash(grid, p, flashers)\n",
    "        for p in flashers:\n",
    "            grid[p] = 0\n",
    "        flashes += len(flashers)\n",
    "    return flashes\n",
    "\n",
    "def check_flash(grid, p, flashers):\n",
    "    \"\"\"Check if grid[p] flashes, and if so, recursively spread.\"\"\"\n",
    "    if grid[p] > 9 and p not in flashers:\n",
    "        flashers.add(p)\n",
    "        for p2 in grid.neighbors(p):\n",
    "            grid[p2] += 1\n",
    "            check_flash(grid, p2, flashers)\n",
    "    \n",
    "answer(11.1, 1591, lambda: simulate_flashes(in11, 100))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- **Part 2**: If you can calculate the exact moments when the octopuses will all flash simultaneously, you should be able to navigate through the cavern. **What is the first step during which all octopuses flash?**\n",
    "\n",
    "I feel a bit bad that I have to copy/paste/edit the whole simulation function, changing just the number of steps and the return. But at least I don't have to copy the  `check_flash` function."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.027 seconds for correct answer: 314\n"
     ]
    }
   ],
   "source": [
    "def simulate_flashes2(grid) -> int:\n",
    "    \"\"\"Simulate octopus flashes and return the first step during which all octopuses flash.\"\"\"\n",
    "    grid = grid.copy() # Don't mutate the original grid\n",
    "    for step in count_from(1):\n",
    "        flashers = set()\n",
    "        for p in grid:\n",
    "            grid[p] += 1\n",
    "        for p in grid:\n",
    "            check_flash(grid, p, flashers)\n",
    "        for p in flashers:\n",
    "            grid[p] = 0\n",
    "        if len(flashers) == len(grid):\n",
    "            return step\n",
    "        \n",
    "answer(11.2, 314, lambda: simulate_flashes2(in11))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"https://pbs.twimg.com/media/FGWQiIuXMAUK_aH?format=jpg&name=medium\" width=400 title=\"@GaryJGrady\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# [Day 12](https://adventofcode.com/2021/day/11): Passage Pathing\n",
    "\n",
    "- **Input**: Each item in the input is a connection between two caves. Big caves are written in uppercase, small caves in lowercase. `start` and `end` are two special caves with the obvious meaning."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "────────────────────────────────────────────────────────────────────────────────────────────────────\n",
      "Puzzle input ➜ 22 lines:\n",
      "────────────────────────────────────────────────────────────────────────────────────────────────────\n",
      "xx-xh\n",
      "vx-qc\n",
      "cu-wf\n",
      "ny-LO\n",
      "cu-DR\n",
      "start-xx\n",
      "...\n",
      "────────────────────────────────────────────────────────────────────────────────────────────────────\n",
      "Parsed representation ➜ 22 tuples:\n",
      "────────────────────────────────────────────────────────────────────────────────────────────────────\n",
      "('xx', 'xh')\n",
      "('vx', 'qc')\n",
      "('cu', 'wf')\n",
      "('ny', 'LO')\n",
      "('cu', 'DR')\n",
      "('start', 'xx')\n",
      "...\n"
     ]
    }
   ],
   "source": [
    "in12 = parse(12, words)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- **Part 1**: **How many paths through this cave system are there that visit small caves at most once?**\n",
    "\n",
    "My approach is as follows:\n",
    "- I'll define a path as a list of cave names: `['start', ..., 'end']`.\n",
    "- I'll construct `neighbors` as a mapping from a cave to the list of caves it connects to.\n",
    "- I'll do depth-first search, starting from the trivial path `['start']` and returning all possible paths.  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.013 seconds for correct answer: 4,167\n"
     ]
    }
   ],
   "source": [
    "Path = List[str]\n",
    "    \n",
    "def search_paths(path, neighbors) -> Iterable[Path]:\n",
    "    \"\"\"All paths that start with `path` and lead to 'end' using `neighbors`.\n",
    "    Small caves can only be visited once.\"\"\"\n",
    "    if path[-1] == 'end':\n",
    "        yield [path]\n",
    "    else:\n",
    "        for cave in neighbors[path[-1]]:\n",
    "            if cave.isupper() or cave not in path:\n",
    "                yield from search_paths(path + [cave], neighbors)\n",
    "\n",
    "neighbors = multimap(in12, symmetric=True)\n",
    "                     \n",
    "answer(12.1, 4167, lambda: quantify(search_paths(['start'], neighbors)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- **Part 2**: After reviewing the available paths, you realize you might have time to visit a single small cave twice.  However, the caves named `start` and `end` can only be visited exactly once each. Given these new rules, **how many paths through this cave system are there?**\n",
    "\n",
    "At first I felt bad that I would again have to copy/paste/edit the code for Part 1. I felt better when I realized that the revised function `search_paths2` would have need to call the original `search_paths`: once a path returns to a small cave for the second time, the remainder of the search should be under the `search_paths` rules, not the `search_paths2` rules."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.317 seconds for correct answer: 98,441\n"
     ]
    }
   ],
   "source": [
    "def search_paths2(path, neighbors) -> Iterable[Path]:\n",
    "    \"\"\"Find all paths that start with `path` and lead to 'end' using `neighbors`.\n",
    "    Small caves can only be visited once, except one of them may be visited twice.\"\"\"\n",
    "    if path[-1] == 'end':\n",
    "        yield [path]\n",
    "    else:\n",
    "        for cave in neighbors[path[-1]]:\n",
    "            if cave.isupper() or cave not in path:\n",
    "                yield from search_paths2(path + [cave], neighbors)\n",
    "            elif cave.islower() and cave != 'start':\n",
    "                yield from search_paths(path + [cave], neighbors)\n",
    "                            \n",
    "answer(12.2, 98441, lambda: quantify(search_paths2(['start'], neighbors)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# [Day 13](https://adventofcode.com/2021/day/13): Transparent Origami\n",
    "\n",
    "- **Input**: The input is a set of dots, e.g. \"`6,10`\", followed by an ordered list of fold instructions, e.g. \"`fold along y=7`\".\n",
    "\n",
    "My `parse` command is not set up to parse two different sections, so I'll ask `parse` only to parse each line into a tuple of atoms. Then I'll further process the items to get two variables:\n",
    "- `dots`: a set of `(x, y)` points, such as `(6, 10)`. \n",
    "- `folds`: a list of fold instructions such as `('fold', 'along', 'y', 7)`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "────────────────────────────────────────────────────────────────────────────────────────────────────\n",
      "Puzzle input ➜ 789 lines:\n",
      "────────────────────────────────────────────────────────────────────────────────────────────────────\n",
      "103,224\n",
      "624,491\n",
      "808,688\n",
      "1076,130\n",
      "700,26\n",
      "55,794\n",
      "...\n",
      "────────────────────────────────────────────────────────────────────────────────────────────────────\n",
      "Parsed representation ➜ 789 tuples:\n",
      "────────────────────────────────────────────────────────────────────────────────────────────────────\n",
      "(103, 224)\n",
      "(624, 491)\n",
      "(808, 688)\n",
      "(1076, 130)\n",
      "(700, 26)\n",
      "(55, 794)\n",
      "...\n"
     ]
    }
   ],
   "source": [
    "in13  = parse(13, atoms)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(123, 456, 'fold', 'along', 'x', 3)"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "atoms('123,456 fold along x=3')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('fold', 'along', 'x', 655),\n",
       " ('fold', 'along', 'y', 447),\n",
       " ('fold', 'along', 'x', 327),\n",
       " ('fold', 'along', 'y', 223),\n",
       " ('fold', 'along', 'x', 163),\n",
       " ('fold', 'along', 'y', 111),\n",
       " ('fold', 'along', 'x', 81),\n",
       " ('fold', 'along', 'y', 55),\n",
       " ('fold', 'along', 'x', 40),\n",
       " ('fold', 'along', 'y', 27),\n",
       " ('fold', 'along', 'y', 13),\n",
       " ('fold', 'along', 'y', 6)]"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dots  = {item for item in in13 if len(item) == 2} \n",
    "folds = [item for item in in13 if len(item) > 2]\n",
    "folds"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The idea of this puzzle is that the dots are on transparent paper, and when following the `fold along y=7` instruction, all the dots below the line `y=7` are reflected above the line: they retain the same distance form the `y=7` line, but their `y` value becomes less than `7`. Similarly, for an `x` fold, all the points to the right of the line are reflected to the left. When we finish the folds, a code message will appear (which we can then use to activate the infrared thermal imaging camera system).\n",
    "\n",
    "- **Part 1**: **How many dots are visible after completing just the first fold instruction on your transparent paper?**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "def fold(dots, instruction) -> Set[Point]: \n",
    "    \"\"\"The set of dots that result from following the fold instruction.\"\"\"\n",
    "    fold, along, x_or_y, line = instruction\n",
    "    if x_or_y == 'x':\n",
    "        return {(line - abs(line - x), y) for (x, y) in dots}\n",
    "    else:\n",
    "        return {(x, line - abs(line - y)) for (x, y) in dots}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.000 seconds for correct answer: 638\n"
     ]
    }
   ],
   "source": [
    "answer(13.1, 638, lambda: len(fold(dots, folds[0])))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- **Part 2**: Finish folding the transparent paper according to the instructions. **What code do you use to activate the infrared thermal imaging camera system?**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.012 seconds for correct answer: CJCKBAPB\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAV0AAADnCAYAAAC9roUQAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjMsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+AADFEAAAHdklEQVR4nO3cwa0cRRQF0DJiRRAW+ZABe+eAHIBFDuydAfmAg/D2s+Bbsjeutmrqvlc952wQX6M3r7pbV2i46jcvLy8DgIyfqhcAeCZCFyBI6AIECV2AIKELECR0AYKELkCQ0AUIEroAQUIXIEjoAgQJXYAgoQsQJHQBgoQuQJDQBQgSugBBQhcgSOgCBAldgCChCxAkdAGChC5AkNAFCBK6AEFCFyBI6AIECV2AIKELECR0AYKELkCQ0AUIEroAQUIXIEjoAgQJXYAgoQsQJHQBgoQuQJDQBQgSugBBQhcgSOgCBAldgCChCxAkdAGChC5AkNAFCBK6AEFCFyBI6AIECV2AIKELECR0AYKELkCQ0AUIEroAQUIXIEjoAgQJXYAgoQsQJHQBgoQuQJDQBQgSugBBQhcgSOgCBAldgCChCxAkdAGChC5AkNAFCBK6AEFCFyBI6AIECV2AIKELECR0AYKELkCQ0AUIEroAQUIXIEjoAgQJXYAgoQsQJHQBgoQuQJDQBQgSugBBQhcgSOgCBP2c/sJf//j79zHGhzHG2zHGpzHG+3/+/O3jj36m0my/3ft3//7q+avXp/r5q96v+/Xrvt/Mm5eXl9R3fTnsX2OMX7768+cxxrsvh77ymUqz/Xbv3/37q+evXp/q5696v+7Xr/t+V6R/Xvgwvj3seP33Dz/4mUqz/Xbv3/37q+evXp/q5696v+7Xr/t+U+nQfXvh71c+U2m23+79u39/9fzV61P9/FXv1/36dd9vKh26ny78/cpnKs32271/9++vnr96faqfv+r9ul+/7vtNpUP3/fj/95OvfX79+498ptJsv937d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X5/p9Khe6UDV90TrH5f6Ez1ft3nex/s2vzq53um+/2dSofulQ5cdU+w+n2hM9X7dZ/vfbBr86uf75nu93cqHbpXOnDVPcHq94XOVO/Xfb73wT53T7f6/k5FQ/dKB666J1j9vtDu+3Wf732wz93Trb6/V+jpAgSlf14AeGpCFyBI6AIECV2AIKELECR0AYKELkCQ0AUIEroAQUIXIEjoAgQJXYAgoQsQJHQBgoQuQJDQBQgSugBBQhcgSOgCBAldgCChCxAkdAGChC5AkNAFCBK6AEFCFyBI6AIECV2AIKELECR0AYKELkCQ0AUIEroAQUIXIEjoAgQJXYAgoQsQJHQBgoQuQJDQBQgSugBBQhcgSOgCBAldgCChCxAkdAGChC5AkNAFCBK6AEFCFyBI6AIECV2AIKELECR0AYKELkCQ0AUIEroAQUIXIEjoAgQJXYAgoQsQJHQBgoQuQJDQBQgSugBBQhcgSOgCBAldgCChCxAkdAGChC5AkNAFCBK6AEFCFyBI6AIECV2AIKELECR0AYKELkCQ0AUIEroAQUIXIEjoAgQJXYAgoQsQJHQBgoQuQJDQBQgSugBBQhcgSOgCBP0HCtkaWp9Y+PYAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "def origami(dots, instructions) -> None:\n",
    "    \"\"\"Follow all the instructions and plot the resulting dots.\"\"\"\n",
    "    for instruction in instructions:\n",
    "        dots = fold(dots, instruction)\n",
    "    plt.scatter(*T(dots), marker='o')\n",
    "    plt.axis('equal'); plt.axis('off'); plt.gca().invert_yaxis()\n",
    "    \n",
    "answer(13.2, \"CJCKBAPB\", lambda: origami(dots, folds) or \"CJCKBAPB\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "I kind of cheated here. I didn't want to write an OCR program, so I relied on my own eyes to look at the dots and see the code.\n",
    "\n",
    "**Note**: My transparent paper was folded 12 times. Is that physically feasible? [Britney Gallivan](https://www.youtube.com/watch?v=AfPDvhKvaa0&) says yes (barely)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# [Day 14](https://adventofcode.com/2021/day/14): Extended Polymerization"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- **Input**: The input is a a polymer template (a string of one-letter element names, such as \"`NNCB`\") followed by a list of pair insertion rules (such as \"`CH -> B`\", meaning that a `B` should be inserted into the middle of each `CH` pair).\n",
    "\n",
    "I'll parse each line of the input into a list of `words` (thus ignoring the \"`->`\" characters); then pick out:\n",
    "- `polymer`: the sole word on the first line.\n",
    "- `rules`: the third through last lines, converted into a dict, like `{'CH': 'B', ...}`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "────────────────────────────────────────────────────────────────────────────────────────────────────\n",
      "Puzzle input ➜ 102 lines:\n",
      "────────────────────────────────────────────────────────────────────────────────────────────────────\n",
      "ONSVVHNCFVBHKVPCHCPV\n",
      "\n",
      "VO -> C\n",
      "VV -> S\n",
      "HK -> H\n",
      "FC -> C\n",
      "...\n",
      "────────────────────────────────────────────────────────────────────────────────────────────────────\n",
      "Parsed representation ➜ 102 tuples:\n",
      "────────────────────────────────────────────────────────────────────────────────────────────────────\n",
      "('ONSVVHNCFVBHKVPCHCPV',)\n",
      "()\n",
      "('VO', 'C')\n",
      "('VV', 'S')\n",
      "('HK', 'H')\n",
      "('FC', 'C')\n",
      "...\n"
     ]
    }
   ],
   "source": [
    "in14    = parse(14, words)\n",
    "polymer = in14[0][0]\n",
    "rules   = dict(in14[2:])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- **Part 1**: Apply 10 steps of pair insertion to the polymer template and find the most and least common elements in the result. **What do you get if you take the quantity of the most common element and subtract the quantity of the least common element?**\n",
    "\n",
    "Pair insertion means inserting the element on the right hand side of a rule between each two-element pair. All two-element substrings are considered as pairs, including overlapping ones (my utility function `pairs` handles this). All insertions happen simultaneously during a step."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.005 seconds for correct answer: 3,259\n"
     ]
    }
   ],
   "source": [
    "def pair_insertion(polymer: str, rules, steps: int) -> str:\n",
    "    \"\"\"Insert elements into polymer according to rules; repeat `steps` times.\"\"\"\n",
    "    for _ in range(steps):\n",
    "         polymer = cat(pair[0] + rules[pair]\n",
    "                       for pair in pairs(polymer)) + polymer[-1]\n",
    "    return polymer\n",
    "\n",
    "def quantity_difference(polymer) -> int:\n",
    "    \"\"\"The count of most common element minus the count of least common element.\"\"\"\n",
    "    counts = Counter(polymer).values()\n",
    "    return max(counts) - min(counts)\n",
    "\n",
    "def pairs(seq: Sequence) -> List[Sequence]: \"All overlapping pairs\"; return windows(seq, 2)\n",
    "\n",
    "assert polymer == 'ONSVVHNCFVBHKVPCHCPV'\n",
    "assert rules['VO'] == 'C'\n",
    "assert pairs('NNCB') == ['NN', 'NC', 'CB']\n",
    "assert pair_insertion('NNCB', rules={'NN': 'C', 'NC': 'B', 'CB': 'H'}, steps=1) == 'NCNBCHB'\n",
    "\n",
    "answer(14.1, 3259, lambda: quantity_difference(pair_insertion(polymer, rules, 10)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- **Part 2**: Apply 40 steps of pair insertion to the polymer template and find the most and least common elements in the result. **What do you get if you take the quantity of the most common element and subtract the quantity of the least common element?**\n",
    "\n",
    "The instructions warn us that the resulting polymer after 40 steps will be *trillions* of elements long. So it isn't feasible to just call `pair_insertion` with steps=40. Instead, I'll employ the same trick as with the lanternfish in Day 6: use a `Counter` of element pairs so that, for example, all the `'NC'` pairs in the polymer are handled simultaneously in one operation, rather than handling each one individually. No matter how many steps we do, there are only 100 distinct element pairs, so iterating over them 40 times should be very fast. \n",
    "\n",
    "Here's an example Counter of element pairs:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Counter({'NN': 1, 'NC': 1, 'CB': 1})"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Counter(pairs('NNCB'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "What letters does this represent? The complication is that the pairs overlap, so, if we added up the counts for all the times that, say, the letter `'C'` appears in keys of the Counter, we'd get 2; but it should be 1. We can divide the sum by 2 to avoid double counting, but the first and last letters in the polymer are *not* double-counted, so we need to add back 1/2 for each of those letters. Fortunately the first and last letters are invariant under pair insertion, so we can do this adjustment at the end; we don't have to do it for each step.\n",
    "\n",
    "So all in all there are three representations of a polymer:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [],
   "source": [
    "Polymer       = str           # e.g. 'NNCB'\n",
    "PairCounter   = Counter[str]  # e.g. Counter({'NN': 1, 'NC': 1, 'CB': 1})\n",
    "LetterCounter = Counter[Char] # e.g. Counter({'N': 2, 'C': 1, 'B': 1})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here's how we convert a PairCounter into a LetterCounter:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [],
   "source": [
    "def letter_counts(pair_ctr: PairCounter, polymer: Polymer) -> LetterCounter:\n",
    "    \"\"\"Return a Counter of the letters in the polymer described by the `pair_ctr`.\"\"\"\n",
    "    letters = set(flatten(pair_ctr))\n",
    "    def letter_count(L) -> int:\n",
    "        return int(sum(pair_ctr[L+M] + pair_ctr[M+L] for M in letters) / 2\n",
    "                   + (L == polymer[0]) / 2 + (L == polymer[-1]) / 2)\n",
    "    return Counter({L: letter_count(L) for L in letters})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Counter({'B': 1, 'C': 1, 'N': 2})"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "letter_counts(Counter(pairs('NNCB')), 'NNCB')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's make sure it works when the first and last letters are the same:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [],
   "source": [
    "assert (letter_counts(Counter(pairs('NNCB')), 'NNCB')\n",
    "     == letter_counts(Counter(pairs('NCBN')), 'NCBN'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now the new function `pair_insertion_difference` can call on `pair_insertion2` to solve Part 2 (as well as Part 1):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.000 seconds for correct answer: 3,259\n",
      "0.003 seconds for correct answer: 3,459,174,981,021\n"
     ]
    }
   ],
   "source": [
    "def pair_insertion2(polymer, rules, steps) -> PairCounter:\n",
    "    \"\"\"Insert elements into polymer according to rules; repeat `steps` times.\n",
    "    Return a Counter of element pairs.\"\"\"\n",
    "    pair_ctr = Counter(pairs(polymer))\n",
    "    for _ in range(steps):\n",
    "        pair_ctr2 = Counter()\n",
    "        for LM in pair_ctr:\n",
    "            pair_ctr2[LM[0] + rules[LM]] += pair_ctr[LM]\n",
    "            pair_ctr2[rules[LM] + LM[1]] += pair_ctr[LM]\n",
    "        pair_ctr = pair_ctr2\n",
    "    return pair_ctr\n",
    "\n",
    "def pair_insertion_difference(polymer, rules, steps):\n",
    "    \"\"\"Most common minus least common after `steps` of pair insertion.\"\"\"\n",
    "    return quantity_difference(letter_counts(pair_insertion2(polymer, rules, steps), polymer))\n",
    "\n",
    "assert Counter(pairs('NNCB')) == Counter({'NN': 1, 'NC': 1, 'CB': 1})\n",
    "assert pair_insertion2('NNCB', rules={'NN': 'C', 'NC': 'B', 'CB': 'H'}, steps=1) == (\n",
    "       Counter({'NC': 1, 'CN': 1, 'NB': 1, 'BC': 1, 'CH': 1, 'HB': 1}))\n",
    "assert letter_counts(Counter({'NC': 1, 'CN': 1, 'NB': 1, 'BC': 1, 'CH': 1, 'HB': 1}), 'NNCB') == (\n",
    "       Counter({'N': 2, 'C': 2, 'B': 2, 'H': 1}))\n",
    "assert pair_insertion_difference('NNCB', rules={'NN': 'C', 'NC': 'B', 'CB': 'H'}, steps=1) == 1\n",
    "\n",
    "answer(14.1,          3259, lambda: pair_insertion_difference(polymer, rules, 10))\n",
    "answer(14.2, 3459174981021, lambda: pair_insertion_difference(polymer, rules, 40))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- **Part 3**: Polymer length?\n",
    "\n",
    "The instructions didn't ask, but I want to know the length of the polymer that was created after 40 steps:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "20,890,720,927,744\n"
     ]
    }
   ],
   "source": [
    "length = total(pair_insertion2(polymer, rules, 40))\n",
    "print(f'{length:,d}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Almost 21 trillion. Good to know."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# [Day 15](https://adventofcode.com/2021/day/15): Chiton\n",
    "\n",
    "- **Input**: The input is a square grid of *risk levels* (each one digit, 1–9) for locations in the cave."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "────────────────────────────────────────────────────────────────────────────────────────────────────\n",
      "Puzzle input ➜ 100 lines:\n",
      "────────────────────────────────────────────────────────────────────────────────────────────────────\n",
      "4249856395422795894919869133487611581179923326874763428673979547991221931142777981153991369468629849\n",
      "5812974178739823463799939791688998895568796557798392761499941349143539572865883254186633218867928826\n",
      "3699989976298596286299499129934993241824395574879938998946914116375199242199151918863674914554714898\n",
      "5682435936794718871685718386458294198391116125679589438794914499278679393779734596558953699438589518\n",
      "7681197997388219696918569664119968498599547892968929425479817979816979144947916716989874825679487436\n",
      "9981166198272997899142698141878643123757515999788822988261499197559193945291512682763935126815448215\n",
      "...\n",
      "────────────────────────────────────────────────────────────────────────────────────────────────────\n",
      "Parsed representation ➜ 100 tuples:\n",
      "────────────────────────────────────────────────────────────────────────────────────────────────────\n",
      "(4, 2, 4, 9, 8, 5, 6, 3, 9, 5, 4, 2, 2, 7, 9, 5, 8, 9, 4, 9, 1, 9, 8, 6, 9, 1, 3, 3, 4, 8, 7, 6, ...\n",
      "(5, 8, 1, 2, 9, 7, 4, 1, 7, 8, 7, 3, 9, 8, 2, 3, 4, 6, 3, 7, 9, 9, 9, 3, 9, 7, 9, 1, 6, 8, 8, 9, ...\n",
      "(3, 6, 9, 9, 9, 8, 9, 9, 7, 6, 2, 9, 8, 5, 9, 6, 2, 8, 6, 2, 9, 9, 4, 9, 9, 1, 2, 9, 9, 3, 4, 9, ...\n",
      "(5, 6, 8, 2, 4, 3, 5, 9, 3, 6, 7, 9, 4, 7, 1, 8, 8, 7, 1, 6, 8, 5, 7, 1, 8, 3, 8, 6, 4, 5, 8, 2, ...\n",
      "(7, 6, 8, 1, 1, 9, 7, 9, 9, 7, 3, 8, 8, 2, 1, 9, 6, 9, 6, 9, 1, 8, 5, 6, 9, 6, 6, 4, 1, 1, 9, 9, ...\n",
      "(9, 9, 8, 1, 1, 6, 6, 1, 9, 8, 2, 7, 2, 9, 9, 7, 8, 9, 9, 1, 4, 2, 6, 9, 8, 1, 4, 1, 8, 7, 8, 6, ...\n",
      "...\n"
     ]
    }
   ],
   "source": [
    "in15 = Grid(parse(15, digits))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- **Part 1**: You start in the top left position, your destination is the bottom right position, and you cannot move diagonally. **What is the lowest total risk of any path from the top left to the bottom right?** (Don't count the risk level of your starting position.)\n",
    "\n",
    "Gary Grady's drawing represents the risk involved in finding a path that avoids bumping into the ceiling above or the chitons below.\n",
    "\n",
    "<img src=\"https://pbs.twimg.com/media/FGrU-PKXIAQ6sa8?format=jpg&name=medium\" width=400 title=\"@GaryJGrady\">\n",
    "\n",
    "I'll use a search that updates a grid of the `cost` of the best known path from start to each point. The cost for each point is initially infinite (because we don't know any paths), and is updated each time we find a better path to the point. Whenever we find a better path to a point, we see if that will lead to a better path for the neighbors."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.890 seconds for correct answer: 687\n"
     ]
    }
   ],
   "source": [
    "def search_grid(grid, start=(0, 0), goal=None) -> int:\n",
    "    \"\"\"The total cost of the best path from start to goal (which defaults to bottom right).\"\"\"\n",
    "    goal      = goal or max(grid) # default bottom right\n",
    "    path_cost = Grid({p: inf for p in grid}) # cost of best known path from start to p\n",
    "    frontier  = {start} # Set of grid points to consider for possible improvement to path_cost\n",
    "    while frontier:\n",
    "        p = frontier.pop()\n",
    "        new_cost = 0 if p is start else (grid[p] + min(path_cost[b] for b in grid.neighbors(p)))\n",
    "        if new_cost < path_cost[p]:\n",
    "            path_cost[p] = new_cost\n",
    "            frontier.update(grid.neighbors(p))\n",
    "    return path_cost[goal]\n",
    "\n",
    "answer(15.1, 687, lambda: search_grid(in15))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- **Part 2**: The entire cave is actually five times larger in both dimensions. Your original map tile repeats to the right and downward; each time the tile repeats, all of its risk levels are 1 higher than the tile immediately up or left of it. However, risk levels above 9 wrap back around to 1. Using the full map, **what is the lowest total risk of any path from the top left to the bottom right?**\n",
    "\n",
    "Here's how to define the full map of the cave:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [],
   "source": [
    "def repeat_grid(grid, repeat=5):\n",
    "    \"\"\"Extend the grid to be `repeat` times larger in both directions.\n",
    "    Values within each repeated block are increased by 1 for each repetition to the right or down,\n",
    "    but values over 9 wrap around to 1.\"\"\"\n",
    "    w, h = grid.width, grid.height \n",
    "    return Grid({(x + xr * w, y + yr * h): clock_mod(grid[x, y] + xr + yr, 9)\n",
    "                 for xr in range(repeat) \n",
    "                 for yr in range(repeat)\n",
    "                 for x, y in grid})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(250000, 10000)"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "full_map = repeat_grid(in15, 5)\n",
    "len(full_map), len(in15)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Node((499, 499))"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "####\n",
    "%run AdventUtils.ipynb\n",
    "\n",
    "class ChitonProblem(Problem):\n",
    "    def actions(self, loc):           return self.grid.neighbors(loc)\n",
    "    def result(self, loc1, loc2):     return loc2\n",
    "    def action_cost(self, s1, a, s2): return self.grid[s2]\n",
    "    def h(self, node): return manhatten_distance(node.state, self.goal) # Never overestimate!\n",
    "    \n",
    "astar_search(ChitonProblem(initial=(0, 0), goal=max(full_map), grid=full_map)).path_cost"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2957"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "_.path_cost"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "With 250,000 points in the full map, `search_grid` takes about 5 minutes (I tried it). That's too slow, so I grabbed the [A* search](https://en.wikipedia.org/wiki/A*_search_algorithm) from my [AoC 2017](https://github.com/norvig/pytudes/blob/main/ipynb/Advent%202017.ipynb) notebook, and supplied it with the proper functions to make a move, compute the cost of a move, and estimate the distance to the goal (the `h_func` or \"heuristic function\"). A* is guaranteed to find an optimal path if the heuristic function never overestimates the cost from a state to the goal, so I will use the [Manhattan distance](https://en.wikipedia.org/wiki/Taxicab_geometry) as my heuristic–this is the same as assuming that every risk level in the remainder of the path will be 1, the lowest possible."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [],
   "source": [
    "from heapq import heappop, heappush\n",
    "\n",
    "def Astar(start, neighbors, h_func, step_cost) -> Tuple[int, list]:\n",
    "    \"\"\"Find a (cost, path) tuple for the lowest-cost path from start to a goal.\n",
    "    A goal is any state `s` such that `h_func(s) == 0`.\"\"\"\n",
    "    frontier  = [(h_func(start), start)] # A priority queue, ordered by path_cost(s) + h(s)\n",
    "    previous  = {start: None}  # start state has no previous state; other states will\n",
    "    path_cost = {start: 0}     # The cost of the best path to a state.\n",
    "    path      = lambda s: ([] if (s is None) else path(previous[s]) + [s])\n",
    "    while frontier:\n",
    "        (f, s) = heappop(frontier)\n",
    "        if h_func(s) == 0:\n",
    "            return path_cost[s], path(s)\n",
    "        for s2 in neighbors(s):\n",
    "            g = path_cost[s] + step_cost(s, s2)\n",
    "            if s2 not in path_cost or g < path_cost[s2]:\n",
    "                heappush(frontier, (g + h_func(s2), s2))\n",
    "                path_cost[s2] = g\n",
    "                previous[s2] = s\n",
    "                \n",
    "def Astar_search_grid(grid)  -> Tuple[int, list]:\n",
    "    \"\"\"The (risk, path) tuple of the best path from upper-left to bottom-right on grid.\"\"\"\n",
    "    goal = max(grid)\n",
    "    def h_func(s): return abs(sum(goal) - sum(s)) # estimated path cost from s to goal\n",
    "    def step_cost(_, s2): return grid[s2]         # cost of moving to s2\n",
    "    return Astar((0, 0), grid.neighbors, h_func, step_cost)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "With A* search the run time is greatly improved, down from 5 minutes to 1 second on Part 2 (and a lesser improvement on Part 1)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.036 seconds for correct answer: 687\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "unsupported operand type(s) for +: 'int' and 'tuple'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-72-3ea12dc0165d>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0manswer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m15.1\u001b[0m\u001b[0;34m,\u001b[0m  \u001b[0;36m687\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;32mlambda\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mAstar_search_grid\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0min15\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0manswer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m15.2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2957\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;32mlambda\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mAstar_search_grid\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfull_map\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m<ipython-input-1-2dcb60951364>\u001b[0m in \u001b[0;36manswer\u001b[0;34m(puzzle, correct, code)\u001b[0m\n\u001b[1;32m      7\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0mpretty\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0;34mf'{x:,d}'\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mis_int\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32melse\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      8\u001b[0m     \u001b[0mstart\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtime\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m     \u001b[0mgot\u001b[0m   \u001b[0;34m=\u001b[0m \u001b[0mcode\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     10\u001b[0m     \u001b[0mdt\u001b[0m    \u001b[0;34m=\u001b[0m \u001b[0mtime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtime\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0mstart\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     11\u001b[0m     \u001b[0mans\u001b[0m   \u001b[0;34m=\u001b[0m \u001b[0mpretty\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mgot\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m<ipython-input-72-3ea12dc0165d>\u001b[0m in \u001b[0;36m<lambda>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0manswer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m15.1\u001b[0m\u001b[0;34m,\u001b[0m  \u001b[0;36m687\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;32mlambda\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mAstar_search_grid\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0min15\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0manswer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m15.2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2957\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;32mlambda\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mAstar_search_grid\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfull_map\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m<ipython-input-64-89dd163598f8>\u001b[0m in \u001b[0;36mAstar_search_grid\u001b[0;34m(grid)\u001b[0m\n\u001b[1;32m     24\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0mh_func\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ms\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mabs\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msum\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mgoal\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0msum\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ms\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# estimated path cost from s to goal\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     25\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0mstep_cost\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0m_\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0ms2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mgrid\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0ms2\u001b[0m\u001b[0;34m]\u001b[0m         \u001b[0;31m# cost of moving to s2\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 26\u001b[0;31m     \u001b[0;32mreturn\u001b[0m \u001b[0mAstar\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mgrid\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mneighbors\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mh_func\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstep_cost\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m<ipython-input-64-89dd163598f8>\u001b[0m in \u001b[0;36mAstar\u001b[0;34m(start, neighbors, h_func, step_cost)\u001b[0m\n\u001b[1;32m     13\u001b[0m             \u001b[0;32mreturn\u001b[0m \u001b[0mpath_cost\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0ms\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mpath\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ms\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     14\u001b[0m         \u001b[0;32mfor\u001b[0m \u001b[0ms2\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mneighbors\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ms\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 15\u001b[0;31m             \u001b[0mg\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mpath_cost\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0ms\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mstep_cost\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ms\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0ms2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     16\u001b[0m             \u001b[0;32mif\u001b[0m \u001b[0ms2\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mpath_cost\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0mg\u001b[0m \u001b[0;34m<\u001b[0m \u001b[0mpath_cost\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0ms2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     17\u001b[0m                 \u001b[0mheappush\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfrontier\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mg\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mh_func\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ms2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0ms2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'int' and 'tuple'"
     ]
    }
   ],
   "source": [
    "answer(15.1,  687, lambda: Astar_search_grid(in15)[0])\n",
    "answer(15.2, 2957, lambda: Astar_search_grid(full_map)[0])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- **Part 3**: Visualization\n",
    "\n",
    "Here we see the two paths on the two grids:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def plot_search_grid(grid, fmt='g-'):\n",
    "    \"\"\"PLot the path from start to goal.\"\"\"\n",
    "    risk, path = Astar_search_grid(grid)\n",
    "    plt.plot(*T(path), fmt); plt.gca().invert_yaxis()\n",
    "    plt.title(f'Path with {len(path) - 1} steps; risk level {risk}')\n",
    "    \n",
    "plot_search_grid(in15)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot_search_grid(full_map)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# [Day 16](https://adventofcode.com/2021/day/16): Packet Decoder\n",
    "\n",
    "- **Input**: The input is a single line containing a sequence of hexadecimal digits, a message using the Buoyancy Interchange Transmission System (BITS). \n",
    "\n",
    "<img src=\"https://pbs.twimg.com/media/FGwEUnEWYAE5SVJ?format=jpg&name=medium\" width=400 title=\"@GaryJGrady\">\n",
    "\n",
    "For now I will leave the input as a string of hex digits:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "in16 = parse(16)[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- **Part 1:** Decode the structure of your hexadecimal-encoded BITS transmission; **what do you get if you add up the version numbers in all packets?**\n",
    "\n",
    "The gist of [the instructions](https://adventofcode.com/2021/day/16) is to consider the hexadecimal sequence as a bit string, divide the bit string into bit fields, and construct nested packets based on the values of the fields. Here are basic types for `Bits` (a bit string) and `Packet` (which contains a version number `V`, a type ID `T`, and a `contents` field which can be either a number or a list of packets), along with functions to convert from a hexadecimal string to a bit string, and from there to an int: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Bits   = str # a string of '0's and '1's\n",
    "Packet = namedtuple('Packet', 'V, T, contents') # V is version; T is type ID\n",
    "\n",
    "def bits_from_hex(hex) -> Bits: \n",
    "    \"\"\"Convert a hexadecimal string into a bit string, making sure each hex digit is 4 bits.\"\"\"\n",
    "    # I could have used just `bin(int(hex, 16))`, except that wouldn't left-zero-pad when needed.\n",
    "    return cat(f'{int(x, 16):04b}' for x in hex)\n",
    "\n",
    "def int2(bits: Bits) -> int: \n",
    "    \"\"\"Convert a bit string into an int.\"\"\"\n",
    "    return int(bits, 2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To parse the bit string into packets, I will have four functions that start with the word `parse_` and return a tuple of two values: the object parsed (either an int or a packet) and the remaining bits that were not parsed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def parse_int(L, bits) -> Tuple[int, Bits]:\n",
    "    \"\"\"Parse an integer from the first L bits; return the int and the remaining bits.\"\"\"\n",
    "    return int2(bits[:L]), bits[L:]\n",
    "\n",
    "def parse_packet(bits) -> Tuple[Packet, Bits]:\n",
    "    \"\"\"Parse a packet; return it and the remaining bits.\"\"\"\n",
    "    V, T, bits = int2(bits[0:3]), int2(bits[3:6]), bits[6:]\n",
    "    parser = parse_literal_packet if T == 4 else parse_operator_packet\n",
    "    return parser(V, T, bits)\n",
    "        \n",
    "def parse_literal_packet(V, T, bits) -> Tuple[Packet, Bits]:\n",
    "    \"\"\"Build a packet with a literal value; return it and the remaining bits.\"\"\"\n",
    "    literal = ''\n",
    "    while True:\n",
    "        prefix, group, bits = bits[0], bits[1:5], bits[5:]\n",
    "        literal += group\n",
    "        if prefix == '0':\n",
    "            return Packet(V, T, int2(literal)), bits\n",
    "\n",
    "def parse_operator_packet(V, T, bits) -> Tuple[Packet, Bits]:\n",
    "    \"\"\"Build a packet with subpackets; return it and the remaining bits.\"\"\"\n",
    "    I, bits = parse_int(1, bits)\n",
    "    L, bits = parse_int((15, 11)[I], bits)\n",
    "    subpackets = []   \n",
    "    if I == 0: # Parse L bits of subpackets\n",
    "        subpacket_bits, bits = bits[:L], bits[L:]\n",
    "        while subpacket_bits:\n",
    "            packet, subpacket_bits = parse_packet(subpacket_bits)\n",
    "            subpackets.append(packet)\n",
    "    else: # Parse L subpackets\n",
    "        for p in range(L):\n",
    "            packet, bits = parse_packet(bits)\n",
    "            subpackets.append(packet)        \n",
    "    return Packet(V, T, subpackets), bits"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we're ready to solve the puzzle by summing up the version numbers, `V`, of all the packets:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def nested_packets(packet) -> Iterator[Packet]: \n",
    "    \"\"\"The packet and all its subpackets.\"\"\"\n",
    "    yield packet\n",
    "    if packet.T != 4: \n",
    "        for p in packet.contents:\n",
    "            yield from nested_packets(p)\n",
    "\n",
    "packet16, _ = parse_packet(bits_from_hex(in16))\n",
    "\n",
    "answer(16.1, 989, lambda: sum(p.V for p in nested_packets(packet16)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This was more code than previous days! Here are some assertions I used to make sure I was on the right track:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "assert (bits_from_hex('D2FE28') == '110100101111111000101000')\n",
    "\n",
    "assert (int2(bits_from_hex('D2FE28')) == 13827624)\n",
    "\n",
    "assert (bits_from_hex('38006F45291200') \n",
    "        == '00111000000000000110111101000101001010010001001000000000')\n",
    "\n",
    "assert (parse_int(4, '011100111') == (7, '00111'))\n",
    "\n",
    "assert (parse_packet('110100101111111000101000') \n",
    "        == parse_literal_packet(6, 4, '101111111000101000')\n",
    "        == (Packet(V=6, T=4, contents=2021), '000'))\n",
    "\n",
    "assert (parse_packet('00111000000000000110111101000101001010010001001000000000')\n",
    "        == (Packet(V=1, T=6, contents=[Packet(V=6, T=4, contents=10), \n",
    "                                       Packet(V=2, T=4, contents=20)]),\n",
    "            '0000000'))\n",
    "\n",
    "assert (parse_packet('11101110000000001101010000001100100000100011000001100000')\n",
    "        == (Packet(V=7, T=3, contents=[Packet(V=2, T=4, contents=1), \n",
    "                                       Packet(V=4, T=4, contents=2), \n",
    "                                       Packet(V=1, T=4, contents=3)]),\n",
    "            '00000'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- **Part 2**: **What do you get if you evaluate the expression represented by your hexadecimal-encoded BITS transmission?**\n",
    "\n",
    "The evaluation rules are that a literal packet evaluates to the number that is its contents, and an operator packet applies an operator determined by the type id (in the `packet.T` field) to the list of values formed by evaluating the subpackets. I put the operators into the `packet_ops` dict."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def eval_packet(packet) -> int:\n",
    "    \"\"\"Evaluate a packet according to the operator rules.\"\"\"\n",
    "    if packet.T == 4:\n",
    "        return packet.contents\n",
    "    else:\n",
    "        vals = [eval_packet(p) for p in packet.contents]\n",
    "        return packet_ops[packet.T](vals)\n",
    "    \n",
    "packet_ops = {0: sum, 1: prod, 2: min, 3: max, \n",
    "              5: lambda v: int(v[0] > v[1]), \n",
    "              6: lambda v: int(v[0] < v[1]), \n",
    "              7: lambda v: int(v[0] == v[1])}\n",
    "\n",
    "answer(16.2, 7936430475134, lambda: eval_packet(packet16))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# [Day 17](https://adventofcode.com/2021/day/17): Trick Shot\n",
    "\n",
    "- **Input**: The input is a short string describing the x and y coordinates of a target area.\n",
    "\n",
    "Because the input is so short, I will copy it literally here instead of reading it from a file. I use `ints` to extract the four integers."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "in17 = ints(\"target area: x=257..286, y=-101..-57\")\n",
    "in17"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The puzzle involves firing a probe and checking if it hits the target area. The probe starts from an initial position with an initial velocity, and traverses a path according to the physics described in the instructions: each time step position is incremented by velocity, but gravity causes it to gain a -1 in `y` velocity; drag causes it lose 1 in `x` velocity.\n",
    "\n",
    "<img src=\"https://pbs.twimg.com/media/FG1Ws7vWYAMTsPf?format=jpg&name=medium\" width=400 title=\"@GaryJGrady\">\n",
    "\n",
    "- **Part 1**: Find the initial velocity that causes the probe to reach the highest `y` position and still eventually be within the target area after some time step. **What is the highest `y` position it reaches on this trajectory?**\n",
    "\n",
    "First I'll define two classes:\n",
    "- `Target` keeps track of the `Xs` and `Ys` that define the target area.\n",
    "- `Probe` keeps track of:\n",
    "  - The `x` and `y` position coordinates\n",
    "  - The `vx` and `vy` velocity values\n",
    "  - A boolean `hit` which is True if the probe hit the target at some point in its path\n",
    "  - The `highest` height it ever reached."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Target:\n",
    "    \"\"\"The target has a range of Xs and Ys coordinates.\"\"\"\n",
    "    def __init__(self, x0, x1, y0, y1): self.Xs, self.Ys = cover(x0, x1), cover(y0, y1) \n",
    "        \n",
    "Probe = namedtuple('Probe', 'x, y, vx, vy, hit, highest', \n",
    "                   defaults=(0, 0, 0,  0,  False, 0))\n",
    "\n",
    "target17 = Target(*in17)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The function `probe_step` simulates the physics of the world for one time step: incrementing the probe's position by its velocity vector,  changing the `xv` velocity due to drag and the `yv` velocity due to gravity, and tracking the `hit` and `highest` values.\n",
    "\n",
    "The function `probe_steps` simulates for multiple time steps; until the probe has passed the target. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def probe_steps(probe, target=target17, do=ignore) -> Probe:\n",
    "    \"\"\"Simulate the probe until it passes the target.\n",
    "    You can optionally `do` something to the probe on each time step.\"\"\"\n",
    "    maxx, miny = max(target.Xs), min(target.Ys)\n",
    "    do(probe)\n",
    "    while probe.x <= maxx and probe.y >= miny:\n",
    "        probe = probe_step(probe, target)\n",
    "        do(probe)\n",
    "    return probe\n",
    "\n",
    "def probe_step(probe, target) -> Probe:\n",
    "    \"\"\"Simulate the physics of the probe for one time step.\"\"\"\n",
    "    x, y, vx, vy, hit, highest = probe\n",
    "    return Probe(x=x + vx, y=y + vy, \n",
    "                 vx=sign(vx) * (abs(vx) - 1), vy=vy - 1,\n",
    "                 hit=hit or (x in target.Xs and y in target.Ys),\n",
    "                 highest=max(highest, y + vy))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def probe_steps(probe, target=target17, do=ignore) -> Probe:\n",
    "    \"\"\"Simulate the probe until it passes the target.\n",
    "    You can optionally `do` something to the probe on each time step.\"\"\"\n",
    "    maxx, miny = max(target.Xs), min(target.Ys)\n",
    "    do(probe)\n",
    "    while probe.x <= maxx and probe.y >= miny:\n",
    "        x, y, vx, vy, hit, highest = probe\n",
    "        probe = Probe(x=x + vx, y=y + vy, \n",
    "                      vx=sign(vx) * (abs(vx) - 1), vy=vy - 1,\n",
    "                      hit=hit or (x in target.Xs and y in target.Ys),\n",
    "                      highest=max(highest, y + vy))\n",
    "        do(probe)\n",
    "    return probe"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "probe_steps(Probe(vx=24, vy=5))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "By experimentation, I found that:\n",
    "- Any `vx<23` will never reach the target (regardless of `vy`).\n",
    "- A `vx=23` value means that the probe will have an `x` velocity of zero when it is inside the width of the target. \n",
    "- Any `vx>23` will eventually pass beyond the target width (and might or might not hit the target along the way). \n",
    "- This is because 23 is the only value that leads to a sequence of decreasing `vx` values adding up to an `x` position that is within the x=257..286 target area: 23 + 22 + 21 + ... + 2 + 1 = 276. (As with Day 7, we're dealing with triangular numbers.)  Here's the demonstration:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "{vx: [sum(range(vx, 0, -1)), sum(range(vx, 0, -1)) in target17.Xs]\n",
    " for vx in [22, 23, 24]}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Specifying `do=print` is useful for experimentation:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "probe_steps(Probe(vx=23, vy=7), do=print)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Once I found the critical `vx=23` value, I figured I could simply vary the `vy` values to find the highest height:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def highest_height(vxs, vys) -> int:\n",
    "    \"\"\"The highest height reached by a probe that hits the target, among all vx and vy values.\"\"\"\n",
    "    probes = [probe_steps(Probe(vx=vx, vy=vy)) for vx in vxs for vy in vys]\n",
    "    return max(probe.highest for probe in probes if probe.hit)\n",
    "    \n",
    "answer(17.1, 5050, lambda: highest_height(vxs=[23], vys=range(150)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "I recognize 5,050 as the 100th triangular number (just as Gauss did, as [legend has it](https://www.americanscientist.org/article/gausss-day-of-reckoning)), and of course the highest height of any trajectory must be a triangular number, because `vy` decreases by one each step.\n",
    "\n",
    "- **Part 2**: **How many distinct initial velocity values cause the probe to be within the target area after some time step?**\n",
    "    \n",
    "I can try a bunch of `vx` and `vy` values. For `vx`, start at the critical 23 value and go up to the maximum of the target area (meaning that the probe hits the right edge of the target area on the first time step). For `vy`, start with a negative value that would hit the bottom of the target area on the first time step, and go up to 100. (Anything more than `vy=100` would end up passing through the target without touching it: the probe's `y` value would be above the target on one step and below it on the next.)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def probe_hits(velocities: Iterable[Point]) -> int:\n",
    "    \"\"\"How many of these velocities cause the probe to hit the target?\"\"\"\n",
    "    return quantify(probe_steps(Probe(vx=vx, vy=vy)).hit \n",
    "                    for vx, vy in velocities)\n",
    "\n",
    "answer(17.2, 2223, lambda:\n",
    "       probe_hits(cross_product(range(23, max(target17.Xs) + 1), \n",
    "                                range(min(target17.Ys), 101))))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- **Part 3**: Visualization\n",
    "\n",
    "I'd like to understand things a bit better with some visualization. The function `plot_probes` plots the target as a black box and plots the paths of various probes with different initial velocities in different colors:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def plot_probes(velocities: List[Point], target=target17) -> None:\n",
    "    \"\"\"Plot the target as a black box and the paths of probes with colored lines.\"\"\"\n",
    "    plt.plot(*box(target.Xs, target.Ys), 'k-', linewidth=4)\n",
    "    for (vx, vy) in velocities:\n",
    "        path = []\n",
    "        probe_steps(Probe(vx=vx, vy=vy), do=path.append)\n",
    "        plt.plot([p.x for p in path], [p.y for p in path], '.:', label=f'({vx}, {vy})')\n",
    "    plt.legend()\n",
    "    \n",
    "def box(Xs, Ys) -> Tuple[List[int], List[int]]:\n",
    "    \"\"\"A tuple of (x_coords, y_coords) to draw a box around the (x, y) points.\"\"\"\n",
    "    x1, x2, y1, y2 = min(Xs), max(Xs), min(Ys), max(Ys)\n",
    "    return [x1, x2, x2, x1, x1], [y1, y1, y2, y2, y1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Below are four paths: two  that hit the target, and two that miss:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    " plot_probes([(24, 5), (32, 0), (34, -5), (36, -10)])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Below we see that `vx=22` is doomed to stall before it reaches the target area; `vx=23` is the critical value that stalls and falls into the target area; and `vx=24` shoots beyond the target area before stalling."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    " plot_probes([(22, 12), (23, 10), (24, 8)])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Below is `vx=23`  paired with three different `vy` velocities:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    " plot_probes([(23, 16), (23, 11), (23, 7)])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "When `vx < vy` the probe's path begins by bending up (e.g. for `(23, 32)`); when `vx = vy` the path is a straight line  (with ever-slowing speed); and when `vx > vy` the path bends down (e.g. for `(23, 8)`). But all paths eventually stall and fall due to drag."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    " plot_probes([(23, 32), (23, 23), (23, 8)])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Below, initial velocity `(23, 100)`, yields the high point of 5050 before stalling and falling into the target:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    " plot_probes([(23, 100)])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note the y-axis on this plot is ten times more than the previous plot. That's why the target box looks so squished and the path slope looks shallow. If we equalize the two axes, we can see how steep the slope is (and why this day is titled \"Trick Shot\"):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    " plot_probes([(23, 100)]); plt.axis('square');"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "I can now see a more efficient way to deal with this problem:\n",
    "  - The movement in  `x` and  `y` are independent, so we can treat them separately:\n",
    "      - For each `vx`, determine in which time step(s) a probe could intersect `target.Xs`.\n",
    "      - For each `vy`, determine in which time step(s) a probe could intersect `target.Ys`.\n",
    "  - Now if a time step has 10 `vx` and 12 `vy` intersects, then that's 120 hits. \n",
    "  - The number of simulations we have to do is the sum of the lengths of `vxs` and `vys`, not their product.\n",
    "  - This technique could have cut the number of simulations from about 50,000 to about 500. \n",
    "  - But 50,000 is a small number and the code runs in well under a second, so we don't need a re-implementation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# [Day 18](https://adventofcode.com/2021/day/18): Snailfish\n",
    "\n",
    "- **Input**: The input is the math homework for some snailfish: each item is a *snailfish number*. A snailfish number is either a regular number (a non-negative integer) or a bracketed pair of two snailfish numbers separated by a comma. \n",
    "\n",
    "For now I'll leave each line of the input as a string:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "in18 = parse(18)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- **Part 1**: Add up all of the snailfish numbers  in the order they appear. **What is the magnitude of the final sum?**\n",
    "\n",
    "From [the Day 18 instructions](https://adventofcode.com/2021/day/18), I determined that I will need to do the following:\n",
    "- **Add** two snailfish numbers.\n",
    "- **Reduce** a snailfish number to simplified form.\n",
    "- **Split** a regular number that is inside a snailfish number.\n",
    "- **Explode** a deeply-nested pair inside a snailfish number.\n",
    "- Compute the **magnitude** of a snailfish number.\n",
    "\n",
    "What representation of snailfish numbers can best handle these operations? I considered three candidates:\n",
    "1. **String**: a string in the same format as the input file. I could manipulate them with regular expressions. <br>But as my friend Jamie Zawinski [famously said](http://regex.info/blog/2006-09-15/247), \"now I have two problems.\"\n",
    "2. **Tree**: a nested tree of pairs, each a Python list of two elements. The input string can be directly converted into this form with `functools.literal_eval`. But **explode** needs to find the \"previous\" and \"next\" numbers–that would require walking up and down the tree, or maintaining next/previous pointers. Sounds complicated.\n",
    "3. **Flat list**: A flat linear list of regular numbers, with annotations giving the nesting level of each number. \n",
    "\n",
    "I decided to go with the flat list. The split and explode operations should be easy. Finding the previous and next number is trivial. However, for the  **magnitude** calculation I think I will want to convert to the nested tree form. \n",
    "\n",
    "I will define these three data types:\n",
    "- `Snum`: a *snailfish number*. Implemented as a mutable flat list of `Num` objects. \n",
    "- `Num`: a *regular number*. A mutable object with two fields: `.n`, the number itself, and `.level`, the nesting level. \n",
    "- `Tree`: a *snailfish number* represented by either an `int` or a list of two `Tree` elements.\n",
    "\n",
    "For example, the snailfish number `[[7,[8,9]],10]` would be represented as:\n",
    "\n",
    "     Snum([Num(7, level=2), Num(8, level=3), Num(9, level=3), Num(10, level=1)])\n",
    "\n",
    "Here are the three type definitions:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Snum(list): \n",
    "    \"\"\"An Snum is a list of Num components.\"\"\"\n",
    "\n",
    "@dataclass()\n",
    "class Num:\n",
    "    \"\"\"A \"regular number\" within an Snum, annotated with its nesting level.\"\"\"\n",
    "    n: int\n",
    "    level: int\n",
    "    \n",
    "Tree = Union[int, List['Tree']] # An int like `1` or a list like `[2,3]` or `[1,[2,3]]`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Below are the four main operations. (I wanted to be sure I got the instructions right, so I copied them mostly verbatim into the docstrings.) Note that `split` and `explode` mutate their argument, and return `True` if they changed the argument. That simplifies the control flow in `snum_reduce`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def snum_add(left: Snum, right: Snum) -> Snum:\n",
    "    \"\"\"To add two snailfish numbers, form a pair from the left and right \n",
    "    parameters of the addition operator. Snailfish numbers must always be reduced.\"\"\"\n",
    "    snum = Snum(Num(x.n, x.level + 1) for x in left + right)\n",
    "    return snum_reduce(snum)\n",
    "\n",
    "def snum_reduce(snum) -> Snum:\n",
    "    \"\"\"Mutate snum until it is in reduced form.\n",
    "    To reduce a snailfish number, you must repeatedly do the first action \n",
    "    that applies to the snailfish number: (1) explode (2) split.\"\"\"\n",
    "    while explode(snum) or split(snum):\n",
    "        continue\n",
    "    return snum\n",
    "\n",
    "def split(snum) -> bool:\n",
    "    \"\"\"If any regular number is 10 or greater, the leftmost such regular number splits.\n",
    "    Mutate the snum and return True if there was a split; False if not.\n",
    "    To split a regular number, replace it with a pair; the left element of the pair \n",
    "    should be the regular number divided by two and rounded down, while the right element \n",
    "    of the pair should be the regular number divided by two and rounded up. \n",
    "    For example, 10 becomes [5,5], 11 becomes [5,6], 12 becomes [6,6], and so on.\"\"\"\n",
    "    i = first(i for i, s in enumerate(snum) if s.n >= 10)\n",
    "    if i is None: \n",
    "        return False\n",
    "    else: # The number to split is snum[i]\n",
    "        level = snum[i].level\n",
    "        L, R  = snum[i].n // 2, (snum[i].n + 1) // 2\n",
    "        snum[i] = Num(L, level + 1)\n",
    "        snum.insert(i + 1, Num(R, level + 1))\n",
    "        return True\n",
    "    \n",
    "def explode(snum) -> bool:\n",
    "    \"\"\"If any pair is nested inside four pairs, the leftmost such pair explodes.\n",
    "    Mutate the snum and return True if there was an explode; False if not.\n",
    "    To explode a pair, the pair's left value is added to the first regular number \n",
    "    to the left of the exploding pair (if any), and the pair's right value is added \n",
    "    to the first regular number to the right of the exploding pair (if any). \n",
    "    Exploding pairs will always consist of two regular numbers. \n",
    "    Then, the entire exploding pair is replaced with the regular number 0.\"\"\"\n",
    "    i = first(i for i, s in enumerate(snum) if s.level > 4)\n",
    "    if i is None:\n",
    "        return False\n",
    "    else: # the exploding pair is: [snum[i], snum[i + 1]]\n",
    "        if i - 1 >= 0: # The pair's left value is added to the number to the left\n",
    "            snum[i - 1].n += snum[i].n\n",
    "        if i + 2 < len(snum): # The pair's right value is added to the number to the right\n",
    "            snum[i + 2].n += snum[i + 1].n\n",
    "        snum[i:i+2] = [Num(0, snum[i].level - 1)] # Replace the pair with a `0`\n",
    "        return True"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here are routines to convert from a string into an `Snum`, and from an `Snum` to a `Tree`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def snum_from_str(snum_str: str) -> Snum:\n",
    "    \"\"\"Convert a string representing a snailfish number into an Snum (a list of Nums).\"\"\"\n",
    "    level = 0\n",
    "    result = []\n",
    "    # Break the string into pieces, e.g. '[[7,8],10]' => '[[', '7', ',', '8', '],', '10', ']'\n",
    "    for piece in re.split(r'(\\d+)', snum_str):\n",
    "        if piece[0] in '0123456789':\n",
    "            result.append(Num(int(piece), level))\n",
    "        else:\n",
    "            level += piece.count('[') - piece.count(']')\n",
    "    return result\n",
    "    \n",
    "def tree_from_snum(snum) -> Tree:\n",
    "    \"\"\"Convert an Snum into a nested tree of two-element pairs.\"\"\"\n",
    "    q = deque(snum)\n",
    "    def grab(level):\n",
    "        return (q.popleft().n if q[0].level == level \n",
    "                else [grab(level + 1), grab(level + 1)])\n",
    "    return grab(level=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Finally, the **magnitude**. I can't see an easy way to directly compute it on an `Snum`, so I'll convert to a `Tree` first."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def snum_magnitude(snum) -> int: \n",
    "    \"\"\"The magnitude of a pair is 3 times the magnitude of its left element \n",
    "    plus 2 times the magnitude of its right element. \n",
    "    The magnitude of a regular number is just that number.\"\"\"\n",
    "    def mag(tree): return tree if is_int(tree) else 3 * mag(tree[0]) + 2 * mag(tree[1])\n",
    "    return mag(tree_from_snum(snum))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now the puzzle solution:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "homework = mapt(snum_from_str, in18)\n",
    "\n",
    "answer(18.1, 4457, lambda: snum_magnitude(functools.reduce(snum_add, homework)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Great! It worked the first time! That justifies my choice of a flat list as the easiest representation for `Snum`.\n",
    "\n",
    "- **Part 2**: **What is the largest magnitude of any sum of two different snailfish numbers from the homework assignment?**\n",
    "\n",
    "My implementation is fast enough that I can try all possibilities:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "answer(18.2, 4784, lambda:\n",
    "       max(snum_magnitude(snum_add(L, R)) \n",
    "           for L in homework \n",
    "           for R in homework if L != R))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# [Day 19](https://adventofcode.com/2021/day/19): Beacon Scanner \n",
    "\n",
    "- **Input**: The input is a sequence of *scanner reports*, where each report is a list of the relative three-dimensional distances to beacons that the scanner can see.\n",
    "\n",
    "I'll `parse` the input into sections, then split each section into lines, ignoring the first line with the scanner number, and parsing the remaining lines of each section into a sequence of  3D points, and collecting them into an object of type `Scanner`, which I define as a subclass of `set`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Scanner(set):\n",
    "    \"\"\"A Scanner is a set of points representing the beacons that the scanner can see.\"\"\"\n",
    "    \n",
    "in19 = parse(19, lambda section: Scanner(mapt(ints, section.splitlines()[1:])), '\\n\\n')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- **Part 1**: Unfortunately, while each scanner can report the positions of all detected beacons relative to itself, the scanners do not know their own position. You'll need to determine the positions of the beacons and scanners yourself. Unfortunately, there's a second problem: the scanners also don't know their rotation or facing direction. Assemble the full map of beacons. **How many beacons are there?**\n",
    "\n",
    "The first thing I want to do is figure out how important efficiency is. `parse` said there are only 36 scanners. On average, how many beacons does each scanner see?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mean(map(len, in19))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Not too bad; I was worried there could be thousands of beacons. There are:\n",
    "  - *S* = 36 scanners\n",
    "  - *B* = 26 mean beacons per scanner\n",
    "  - 24 orientations of each scanner (per the instructions)\n",
    "\n",
    "At first I was confused: I thought that if there are 3! permutations of  `(x, y, z)`, and 2<sup>3</sup> ways to have or not have a minus sign on each dimension, then there should be 6 × 8 = 48 different orientations. But the instructions say 24. I decided that if you don't permute (x, y, z), and just negate the x component, that's equivalent to a mirror image, which we don't want. You can get a mirroring with a minus sign on any of the three components, or by reversing the order of the components (e.g. `(x, y, z)` going to `(z, y, x)`) but not if you just rotate the order (e.g. `(x, y, z)` going to `(z, x, y)`). Two mirrorings reverse things back to normal, so the 24 valid orientations are those with a rotation and an even number of minus signs, or a reversal in the ordering and an odd number of minus signs. \n",
    "\n",
    "Since I'm going to be applying these transforms often, I will compile them into functions. I'll do that by constructing strings and then applying `eval` to each string."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Point3D = Tuple[int, int, int] # A 3D Point, like (1, 2, -3).\n",
    "\n",
    "def transformers(signs, orders) -> List[Callable]: \n",
    "    \"\"\"Strings that define 3D transformations of signs and variable ordering.\"\"\"\n",
    "    return [transformer(f'({a}{x}, {b}{y}, {c}{z})')\n",
    "            for a,b,c in signs for x,y,z in orders]\n",
    "\n",
    "def transformer(transform: str) -> Callable:\n",
    "    \"\"\"Turn this transformer string into a callable function.\"\"\"\n",
    "    fn = eval('lambda points: {' + transform + ' for x, y, z in points}')\n",
    "    fn.__name__ = 'Orient' + transform\n",
    "    return fn\n",
    "              \n",
    "\n",
    "orient_fns  = (transformers(['   ', '-- ', ' --', '- -'], ['xyz', 'yzx', 'zxy']) + \n",
    "               transformers(['---', '-  ', ' - ', '  -'], ['zyx', 'xzy', 'yxz']))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here's a test applying the 24 orientation functions to a tiny set `B` of 2 beacons:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "assert len(orient_fns) == 24\n",
    "\n",
    "B = {(1, 2, 3), (-4, -5, -6)}\n",
    "\n",
    "assert [(fn.__name__, fn(B)) for fn in orient_fns] == [\n",
    " ('Orient( x,  y,  z)', {(-4, -5, -6), (1, 2, 3)}),\n",
    " ('Orient( y,  z,  x)', {(-5, -6, -4), (2, 3, 1)}),\n",
    " ('Orient( z,  x,  y)', {(-6, -4, -5), (3, 1, 2)}),\n",
    " ('Orient(-x, -y,  z)', {(-1, -2, 3), (4, 5, -6)}),\n",
    " ('Orient(-y, -z,  x)', {(-2, -3, 1), (5, 6, -4)}),\n",
    " ('Orient(-z, -x,  y)', {(-3, -1, 2), (6, 4, -5)}),\n",
    " ('Orient( x, -y, -z)', {(-4, 5, 6), (1, -2, -3)}),\n",
    " ('Orient( y, -z, -x)', {(-5, 6, 4), (2, -3, -1)}),\n",
    " ('Orient( z, -x, -y)', {(-6, 4, 5), (3, -1, -2)}),\n",
    " ('Orient(-x,  y, -z)', {(-1, 2, -3), (4, -5, 6)}),\n",
    " ('Orient(-y,  z, -x)', {(-2, 3, -1), (5, -6, 4)}),\n",
    " ('Orient(-z,  x, -y)', {(-3, 1, -2), (6, -4, 5)}),\n",
    " ('Orient(-z, -y, -x)', {(-3, -2, -1), (6, 5, 4)}),\n",
    " ('Orient(-x, -z, -y)', {(-1, -3, -2), (4, 6, 5)}),\n",
    " ('Orient(-y, -x, -z)', {(-2, -1, -3), (5, 4, 6)}),\n",
    " ('Orient(-z,  y,  x)', {(-3, 2, 1), (6, -5, -4)}),\n",
    " ('Orient(-x,  z,  y)', {(-1, 3, 2), (4, -6, -5)}),\n",
    " ('Orient(-y,  x,  z)', {(-2, 1, 3), (5, -4, -6)}),\n",
    " ('Orient( z, -y,  x)', {(-6, 5, -4), (3, -2, 1)}),\n",
    " ('Orient( x, -z,  y)', {(-4, 6, -5), (1, -3, 2)}),\n",
    " ('Orient( y, -x,  z)', {(-5, 4, -6), (2, -1, 3)}),\n",
    " ('Orient( z,  y, -x)', {(-6, -5, 4), (3, 2, -1)}),\n",
    " ('Orient( x,  z, -y)', {(-4, -6, 5), (1, 3, -2)}),\n",
    " ('Orient( y,  x, -z)', {(-5, -4, 6), (2, 1, -3)})\n",
    "]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "That looks good. The next step is to find pairs of scanners that can see the same beacons. The instructions say that every scanner has at least one other scanner with which it shares at least 12 detected beacons. How can I find the matches, and do it efficiently? Some options:\n",
    "1. For every pair of scanners, I could apply every orientation transformation to *one of them, then try every alignment of a point in that transformation to a point in the other scanner, then count the number of matching points with that alignment, and see if the count exceeds 12.  Total complexity: 24 *S*<sup>2</sup> *B*<sup>3</sup> operations.\n",
    "2. For every scanner, compute the distance between each pair of points. (This set of distances will be invariant under orientation transformations, so we've saved a factor of 24.) Now compare distance sets between pairs of scanners and see which ones exceed 12×11/2 = 66 matches. Total complexity: *S* *B*<sup>2</sup> + *S*<sup>2</sup>*B*<sup>2</sup> operations. (Note: we actually store squared distances, not distances, to avoid possible round-off error and to save the runtime cost of computing square roots.)\n",
    "\n",
    "Option 2 it is!  The algorithm will be:\n",
    "\n",
    "     keep track of lists of `aligned` and `unaligned` scanners\n",
    "     add the first unaligned scanner to the list of aligned scanners\n",
    "     whenever an aligned scanner is added:\n",
    "         check to see if each unaligned can scanner can be aligned with it\n",
    "     while there are unaligned scanners:\n",
    "         pop off one unaligned scanner S\n",
    "         find an unaligned scanner R that c\n",
    "         \n",
    "**NOTE: I started below, but did not complete the solution.**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def align_scanners(scanners, k=12) -> Set[Point3D]:\n",
    "    \"\"\"Align all scanners\"\"\"\n",
    "    for S in scanners:\n",
    "        S.distances2 = {distance2(p, p2) for p, p2 in combinations(S, 2)}\n",
    "    aligned, unaligned = [], scanners\n",
    "    return align_scanner(unaligned.pop(), aligned, unaligned, k)\n",
    "\n",
    "def add_aligned_scanner(S, aligned, unaligned, k) -> List[Scanner]:\n",
    "    \"\"\"Add S to aligned; recursively align other scanners with S; return aligned.\"\"\"\n",
    "    aligned.append(S)\n",
    "    unaligned.remove(S)\n",
    "    for R in unaligned.copy():\n",
    "        if R not in aligned and len(S.distances2 & R.distances2) >= k * (k - 1) // 2:\n",
    "            add_aligned_scanner(align(R, S), aligned, unaligned, k)\n",
    "                        \n",
    "def distance2(p: Point3D, p2: Point3D) -> int:\n",
    "    \"\"\"Squared distance between two 3D points.\"\"\"\n",
    "    return (p[0]-p2[0]) ** 2 + (p[1]-p2[1]) ** 2 + (p[2]-p2[2]) ** 2\n",
    "\n",
    "def align(R: Scanner, S: Scanner, k=12) -> Scanner:\n",
    "    \"Orient and offset Scanner R to align with Scanner S; return the new version of R.\"\n",
    "    for orient in orient_fns:\n",
    "        R2 = orient(R)\n",
    "        for offset in offsets(S1, A):\n",
    "            S2 = Scanner(add3D(s, offset) for s in S)\n",
    "            if len(S2 & A) >= k:\n",
    "                S2.distances2 = S.distances2\n",
    "                return S2\n",
    "    raise ValueError(\"Can't align scanners\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now that we can find two scanners that are alignable, we have to actually align them:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def offsets(S, A) -> Iterator[Point3D]:\n",
    "    \"\"\"All offsets that would make some point in S align with some point in A.\"\"\"\n",
    "    return (minus3D(a, s) for s in S for a in A)\n",
    "\n",
    "def minus3D(A: Point3D, B: Point3D) -> Point3D:\n",
    "    return (A[0] - B[0], A[1] - B[1], A[2] - B[2])\n",
    "\n",
    "def add3D(A: Point3D, B: Point3D) -> Point3D:\n",
    "    return (A[0] + B[0], A[1] + B[1], A[2] + B[2])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# [Day 20](https://adventofcode.com/2021/day/20): Trench Map\n",
    "\n",
    "- **Input**: The input is in two sections: first, a 512-character string describing an image enhancement algorithm; second a grid of pixels depicting an image.\n",
    "\n",
    "I'll parse the file into two sections, calling the first segment `rules` and the second `image`. Then I'll convert the `image` to a `Grid`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "rules, image = in20 = parse(20, sep='\\n\\n')\n",
    "image = Grid(image.splitlines())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- **Part 1**: Start with the original input image and apply the image enhancement algorithm twice, being careful to account for the infinite size of the images. **How many pixels are lit in the resulting image?**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**It is a truth universally acknowledged,** that an Eric Wastl in possession of an Advent of Code, must be in want of a [Life](https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life) puzzle. This is it! The \"image enhancement algorithm\" defines how pixels turn on and off. I've done Life before, e.g. [here](Life.ipynb) and [here](https://adventofcode.com/2020/day/17). This should be easy! \n",
    "\n",
    "**Unfortunately**, there's a twist: spontaneous generation. I was *shocked* to find  `rules[0] == '#'` in my enhancement rules. (That wasn't the case for the example in the instructions.)  This means that every dark pixel in the middle of empty dark space will spontaneously become  light pixel in the next generation, resulting in an infinite number of light pixels.\n",
    "\n",
    "<img src=\"https://pbs.twimg.com/media/FHE69USXsAI8pg2?format=jpg&name=medium\" width=400 title=\"@GaryJGrady\">\n",
    "\n",
    "**Fortunately**, `rules[255] == '.'`, which means that almost all of the infinite number of light pixels will become dark again in the following generation. It's as if we convert between a positive image and a negative on each enhancement.\n",
    "\n",
    "Therefore, my strategy will be:\n",
    "- In generation 0 (and every even generation) we will have a finite set of *light* pixels. Use them to generate a finite set of *dark* pixels.\n",
    "- In generation 1 (and every odd generation), we will have a finite set of *dark* pixels. Use them to generate a finite set of *light* pixels.\n",
    "- Generating a dark pixel requires at least one neighboring light pixel, and generating a light pixel requires at least one neighboring dark pixel. So to know what pixels to consider as candidates for the next generation, I can use the current set of pixels and their neighbors. So I won't need the `Grid`, just the `set` of light pixels in it. I'll create a subclass of `set` called `Pixels` which is just like `set` but keeps track of the `.color` of pixels, light or dark."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "light  = '#'\n",
    "dark   = '.'\n",
    "\n",
    "class Pixels(set):\n",
    "    \"\"\"A set of pixel Points; self.color says if they are light or dark.\"\"\"\n",
    "    def __init__(self, pixels, color=light):\n",
    "        self.update(pixels)\n",
    "        self.color = color\n",
    "\n",
    "def enhance(pixels, n=1, rules=rules) -> Pixels:\n",
    "    \"\"\"The set of pixels that result from applying enhancement rules `n` times.\"\"\"\n",
    "    for generation in range(n):\n",
    "        candidate_pixels = set(flatten(map(neighborhood, pixels)))\n",
    "        negative = dark if pixels.color == light else light\n",
    "        pixels = Pixels((p for p in candidate_pixels\n",
    "                         if rules[pixel_sum(p, pixels)] == negative),\n",
    "                        negative)\n",
    "    return pixels\n",
    "\n",
    "def pixel_sum(point, pixels):\n",
    "    \"\"\"The sum of the 9 pixels in the neighborhood. If `pixels.color == light`,\n",
    "    add up the neighborhood points that are in `pixels`; if not,\n",
    "    add up the neighborhood points that are not in `pixels`.\"\"\"\n",
    "    return sum((256 >> i) * ((p in pixels) == (pixels.color == light))\n",
    "               for i, p in enumerate(neighborhood(point)))\n",
    "\n",
    "def neighborhood(point) -> List[Point]:\n",
    "    \"The nine points surrounding `point` (including `point` itself).\"\n",
    "    (x, y) = point\n",
    "    return [(x-1, y-1), (x, y-1), (x+1, y-1), \n",
    "            (x-1, y),   (x, y),   (x+1, y), \n",
    "            (x-1, y+1), (x, y+1), (x+1, y+1)]\n",
    "\n",
    "pixels = Pixels(p for p in image if image[p] == light) # pixels in my image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "answer(20.1, 5437, lambda: len(enhance(pixels, 2)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- **Part 2**: Start again with the original input image and apply the image enhancement algorithm 50 times. **How many pixels are lit in the resulting image?**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "answer(20.2, 19340, lambda: len(enhance(pixels, 50)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# [Day 21](https://adventofcode.com/2021/day/21): Dirac Dice\n",
    "\n",
    "\n",
    "We're playing a two-player game with the following rules: On each turn a player rolls the die three times, adds them up, and moves forward that many spaces on a circular board consisting of spaces marked 1 to 10; space 10 wraps around to 1. The player increases their score by the space they land on; first player to 1000 points wins.\n",
    "\n",
    "- **Input**: My input is the text \"`Player 1 starting position: 5 \\ Player 2 starting position: 6`\".\n",
    "\n",
    "I won't bother to read this from the file; I'll just translate it into this statement:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "start_positions = (5, 6)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- **Part 1**: Play a practice game using the deterministic 100-sided die. The moment either player wins, **what do you get if you multiply the score of the losing player by the number of times the die was rolled during the game?**\n",
    "\n",
    "I'll implement a deterministic die as an iterator: `cycle(range(1, 101)` returns 1 first, then 2, etc. up to 100, then goes back to 1. My function `play_dice` keeps track of positions and scores for both players and returns the product of the number of dice rolled times the loser's score."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def play_dice(die: Iterator, positions=start_positions, target=1000) -> int:\n",
    "    \"\"\"Play the dice game with the given die until someone scores `target`.\n",
    "    Return total_number_of_dice_rolled * loser_score.\"\"\"\n",
    "    positions = list(positions)\n",
    "    scores = [0, 0]\n",
    "    for turn in count_from(1):\n",
    "        player = (turn - 1) % 2\n",
    "        roll = next(die) + next(die) + next(die)\n",
    "        positions[player] = clock_mod(positions[player] + roll, 10)\n",
    "        scores[player] += positions[player]\n",
    "        if scores[player] >= 1000:\n",
    "            return 3 * turn * min(scores)\n",
    "            \n",
    "answer(21.1, 1002474, lambda: play_dice(die=cycle(range(1, 101))))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- **Part 2**: This time we play with a quantum die. Using your given starting positions, determine every possible outcome. **Find the player that wins in more universes; in how many universes does that player win?**\n",
    "\n",
    "A three-sided quantum die  splits the universe into three copies every time it is rolled: one where the outcome of the roll was 1, one where it was 2, and one where it was 3. We need to track all these universes. But for this game a player only needs 21 points to win; not 1000.\n",
    "\n",
    "The instructions warn us that there will be *trillions* of universes, so I'm concerned about efficiency. I must avoid enumerating all possibile universes one by one. I should either use a `Counter` of game states (as with the lanternfish in Day 6), or use dynamic programming (which I can implement with caching on a recursive function). \n",
    "\n",
    "I decided to go with the recursive function. I'll define `play_dice2(pos1, pos2, score1, score2, target)` as a function that returns a tuple of (number of wins, number of losses) for player 1, given the positions of the two players, the scores of the two players, and the target winning score, under the assumption that it is player 1's turn to roll. Note that in the recursive call to `play_dice2` it is player 2's turn, so the arguments and return values are swapped. I did it this way, rather than have two-element sequences for the positions and scores, because this way cuts in half the number of different game states I have to track."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dice3_rolls = mapt(sum, cross_product([1, 2, 3], repeat=3))\n",
    "\n",
    "@cache\n",
    "def play_dice2(pos1, pos2, score1, score2, target) -> Tuple[int, int]:\n",
    "    \"\"\"The number of (winning universes, losing universes) for player 1,\n",
    "    given that player 1 is at `pos1` with `score1` points (and likewise for player 2).\"\"\"\n",
    "    wins, losses = 0, 0\n",
    "    if score2 >= target: # Player 2 has won; record a loss for player 1\n",
    "        losses += 1 \n",
    "    else:                # Player 1 takes their turn; then count wins and loses for the game\n",
    "        for roll in dice3_rolls:\n",
    "            newpos1 = clock_mod(pos1 + roll, 10)\n",
    "            roll_losses, roll_wins = play_dice2(pos2, newpos1, score2, score1 + newpos1, target)\n",
    "            wins += roll_wins\n",
    "            losses += roll_losses\n",
    "    return wins, losses\n",
    "\n",
    "answer(21.2, 919758187195363, lambda: max(play_dice2(*start_positions, 0, 0, 21)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "I had some ideas to make this more efficient (such as iterating over a Counter of `dice3_rolls`) but it is fast enough as is.\n",
    "\n",
    "How many different game states did we explore?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "play_dice2.cache_info()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Only 24,841 distinct game states."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# [Day 22](https://adventofcode.com/2021/day/22): Reactor Reboot\n",
    "\n",
    "- **Input**: The input is a list of steps for rebooting the ship's reactor, such as \"`on x=10..12,y=10..12,z=10..12`\".\n",
    "\n",
    "I'll parse each line into a tuple consisting of either `\"on\"` or `\"off\"` followed by three ranges (using `cover` because the high end is inclusive):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Step = Tuple[str, range, range, range]\n",
    "\n",
    "def parse_reboot_step(line: str) -> Step:\n",
    "    \"\"\"Parse a line into a reboot step description.\"\"\"\n",
    "    x1, x2, y1, y2, z1, z2 = ints(line)\n",
    "    return (line.split()[0], cover(x1, x2), cover(y1, y2), cover(z1, z2))\n",
    "\n",
    "in22 = parse(22, parse_reboot_step)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The intent of each step is to turn on or off all the individual cubes in the reactor core that are within the *cuboid* specified by the three ragnes. So a 3x3x3 cuboid specifies 27 cubes.\n",
    "\n",
    "- **Part 1**: Execute the reboot steps. Afterward, considering only cubes in the region x=-50..50,y=-50..50,z=-50..50, **how many cubes are on?**\n",
    "\n",
    "[I have a bad feeling about this](https://www.youtube.com/watch?v=S74rvpc6W60). The example in the instructions has a small enough number of cubes that we can just brute-force enumerate them. But I suspect in Part 2 there will be trillions of cubes and brute force willl no longer work. [It's a trap!](https://www.youtube.com/watch?v=4F4qzPbcFiA) \n",
    "\n",
    "Still, I'll go ahead and do the brute-force enumeration for Part 1: for each step that describes a cuboid that falls within the region, form the set of all the cubes in the cuboid, and add or subtract them from a running set of `cubes`.  Each cube is denoted by a single `(x, y, z)` triple of integer coordinates. Note that it is guaranteed that every step describes a cuboid that is either entirely within or entirely outside the position range -50..50 on all three dimensions, so I only need to check one of the three dimensions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def count_cubes(steps, region=cover(-50, 50)) -> int:\n",
    "    \"\"\"Follow `steps` to turn on or off those cubes that are in `region`, and count 'on' cubes.\"\"\"\n",
    "    cubes = set()\n",
    "    for (flip, xs, ys, zs) in steps:\n",
    "        if xs.start < region.start or xs.stop > region.stop:\n",
    "            pass # step is outside the region\n",
    "        elif flip == \"on\":\n",
    "            cubes |= set(cross_product(xs, ys, zs))\n",
    "        else:\n",
    "            cubes -= set(cross_product(xs, ys, zs))\n",
    "    return len(cubes)  \n",
    "\n",
    "answer(22.1, 533863, lambda: count_cubes(in22))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- **Part 2**: Starting again with all cubes off, execute all reboot steps. Afterward, considering all cubes, **how many cubes are on?**\n",
    "\n",
    "Well, I was *wrong*. There aren't going to be *trillions* of cubes; there are going to be *quadrillions*. \n",
    "\n",
    "I will need a way to handle steps without explicitly enumerating the cubes. If no cuboids intersected, it would be easy: for each \"on\" cuboid, multiply the lengths of the three sides to get the volume, then add up the volumes. But the intersections complicate things.\n",
    "\n",
    "I'll start with some [exploratory data analysis](https://en.wikipedia.org/wiki/Exploratory_data_analysis)  to answer some questions.\n",
    "\n",
    "**Question**: how many of the steps are \"on\"?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Counter(step[0] for step in in22)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Answer**: There are 420 steps, of which about 3/4 are \"on\" steps.\n",
    "\n",
    "**Question**: How big are the cuboids? "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "volumes = [len(x) * len(y) * len(z) / 1e12 for (on, x, y, z) in in22]\n",
    "plt.hist(volumes, rwidth=0.8, bins=36, \n",
    "         label=f'cuboid volume in trillions (mean {mean(volumes):.1f})')\n",
    "plt.legend();"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Answer**: An average cuboid consists of 7.6 trillion cubes. A big one has 35 trillion, but most are under 10 trillion. \n",
    "\n",
    "**Question**: How long are the sides of the cuboids?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "lengths = [[len(step[i]) for step in in22] for i in (1,2,3)]\n",
    "plt.hist(lengths, bins=25, label=list('xyz'))\n",
    "plt.legend();"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Answer**: The histogram above breaks out the lengths of the sides of the cuboids by x, y, and z dimensions. It looks like a roughly normal distribution (although noisy) in each dimension with mean around 20,000, except that we can see a spike just above length 0 for the 20 small cuboids that are used in Part 1 (plus a few other cuboids that happen to have small ranges in one of the `x` or `z` dimensions, but not `y`).\n",
    "\n",
    "**Question**: Can I visualize the actual cuboids?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure(figsize=(6, 6)) # Make it square\n",
    "for (on, xs, ys, zs) in in22:\n",
    "    plt.plot(*box(xs, ys), '-')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Answer**: The plot above gives a 2D projection into the XY plane. (You could also project to XZ or YZ planes, but I don't think that would be very different. To do 3D you'd need animation. A 3D VR headset would help.) \n",
    "\n",
    "We can also look more closely at a sample of every tenth cuboid, this time in the YZ plane:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure(figsize=(6, 6))\n",
    "for (on, xs, ys, zs) in in22[::10]:\n",
    "    plt.plot(*box(ys, zs), '-')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Question**: What's a good strategy for counting cubes?\n",
    "\n",
    "**Answer**: You can see in the plots above that when two rectangles intersect, they can leave non-rectangular pieces (and in 3 dimensions, non-cuboid pieces). That's the difficulty. The key is to break these pieces up into smaller pieces that are all cuboids.  The algorithm will be:\n",
    "\n",
    "1. Find all the **split points** in the first dimension–values of `x` that start or end a cuboid. Create a list of **bins**, one covering the range between each pair of adjacent split points.\n",
    "\n",
    "2. Assign each step to the bin(s) it overlaps. For example (in 2 dimensions), suppose that step 1 is a 7x4 \"on\" step and step 2 is a 6x4 \"off\" step, and that they span the  locations shown below left. When we split on `x`  we  get the three bins shown below right (all 1s in the left bin, all 2s in the right bin, and both 1 and 2 in the middle bin):\n",
    "\n",
    "         1111111                   1111  111\n",
    "         1111111                   1111  111\n",
    "         1111222222                1111  222  222\n",
    "         1111222222                1111  222  222\n",
    "             222222                      222  222\n",
    "             222222                      222  222\n",
    "\n",
    "\n",
    "3. Recurse if there are more dimensions to split on. In the example, we need to split on `y`.  The two outside bins aren't split (because they only have one step), but the middle bin is split into 3 bins. Notice that each bin is a rectangle (and in 3 dimensions, each bin would be a cuboid). \n",
    "\n",
    "               111\n",
    "         1111  111\n",
    "         1111\n",
    "         1111  222  222\n",
    "         1111  222  222\n",
    "                    222\n",
    "               222  222\n",
    "               222\n",
    "               \n",
    "\n",
    "4. For each bin that is \"on\", multiply their side lengths to get their volume, and add up the volumes to get the total number of \"on\" cubes. In the example, the \"on\" bins (with step 1) are one 4x4 cuboid and one 3x2 cuboid, giving a total of 22 cubes.\n",
    "\n",
    "For the real input data, I would probably end up with a million or so bins. Adding up the volumes of a million bins will be faster than enumerating a quadrillion cubes.\n",
    "             \n",
    "Below is the implementation. For the `Bin` data type, I'll have a field for the dimension we are splitting on; one for the range of values covered in that dimension by the bin; and one for a list of  `kids`: At first it holds a list of all the steps that at least partially overlap the bin, but then (for the `x` and `y` dimensions but not `z`) that list of steps will be replaced by a list of bins."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "@dataclass\n",
    "class Bin:\n",
    "    dim:   int   # 1, 2, or 3 for x, y, or z dimension\n",
    "    range: range # locations that are covered by this bin, on the given dimension\n",
    "    kids:  Union[List[Step], List['Bin']] # Steps that overlap this bin (may be converted to bins)\n",
    "\n",
    "def bins_from_steps(steps, dims=(1, 2, 3)) -> List[Bin]:\n",
    "    \"\"\"Convert the steps to a list of bins.\"\"\"\n",
    "    # (1) Find all the split points in the first dimension. Create a list of bins.\n",
    "    dim, *more_dims = dims\n",
    "    splits = sorted(set(flatten((step[dim].start, step[dim].stop) for step in steps)))\n",
    "    bins   = [Bin(dim, range(*pair), []) for pair in pairs(splits)]\n",
    "    # (2) Assign each step to the bin(s) it overlaps.\n",
    "    for bin in bins:\n",
    "        bin.kids = [step for step in steps if overlaps(bin.range, step[dim])]\n",
    "    # (3) Recurse if there are more dimensions\n",
    "    if more_dims:\n",
    "        for bin in bins:\n",
    "            bin.kids = bins_from_steps(bin.kids, more_dims)\n",
    "    return bins\n",
    "\n",
    "def overlaps(A: range, B: range) -> bool:\n",
    "    \"\"\"Do these ranges overlap?\"\"\"\n",
    "    return (B.start <= A.start < B.stop) or (A.start <= B.start < A.stop)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here is the example, showing the three bins at the `x` level, and five at the `y` level:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "steps2 = [('on',  range(0, 7), range(0, 4)), \n",
    "          ('off', range(4, 9), range(2, 6))]\n",
    "\n",
    "assert (bins_from_steps(steps2, (1, 2)) == \n",
    "        [Bin(1, range(0, 4), \n",
    "             [Bin(2, range(0, 4), [('on', range(0, 7), range(0, 4))])]), \n",
    "         Bin(1, range(4, 7), \n",
    "             [Bin(2, range(0, 2), [('on', range(0, 7), range(0, 4))]), \n",
    "              Bin(2, range(2, 4), [('on', range(0, 7), range(0, 4)), \n",
    "                                   ('off', range(4, 9), range(2, 6))]), \n",
    "              Bin(2, range(4, 6), [('off', range(4, 9), range(2, 6))])]), \n",
    "         Bin(1, range(7, 9), \n",
    "             [Bin(2, range(2, 6), [('off', range(4, 9), range(2, 6))])])])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The test passes! The only thing that remains is to count up the number of cubes in the \"on\" cuboids. I'll define `count_cubes2` so that it can handle Part 1 as well as Part 2:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def count_cubes2(steps, region=None) -> int:\n",
    "    \"\"\"Follow `steps` to turn on or off cubes, and count 'on' cubes.\"\"\"\n",
    "    if region:\n",
    "        steps = [s for s in steps if s[1].start >= region.start and s[1].stop <= region.stop]\n",
    "    return sum(count_cubes_in_bin(bin) for bin in bins_from_steps(steps))\n",
    "\n",
    "def count_cubes_in_bin(bin) -> int:\n",
    "    \"\"\"How many \"on\" cubes are in this bin? Recurse down to `dim == 3`, keeping track of \n",
    "    the lengths of all three sides, and return the product for bins that are \"on\".\"\"\"\n",
    "    N = len(bin.range)\n",
    "    if bin.dim < 3:\n",
    "        return N * sum(map(count_cubes_in_bin, bin.kids))\n",
    "    elif bin.kids and (bin.kids[-1][0] == 'on'):\n",
    "        return N\n",
    "    else:\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's go ahead and solve the complete puzzle without any more tests:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "answer(22.1, 533863, lambda: count_cubes2(in22, region=cover(-50, 50)))\n",
    "answer(22.2, 1261885414840992, lambda: count_cubes2(in22))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We got the right answers. Part 1 runs 50 times faster; Part 2 takes over 5 seconds. One idea to make it faster: I  compare every step to every bin. This could probably be speeded up with a binary search. I chose not to do that because binary searches are notorious for off-by-one errors; doubly so when searching over ranges of numbers, not a single number."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# [Day 23](https://adventofcode.com/2021/day/23): Amphipod\n",
    "\n",
    "**NOTE: I started below, but did not complete the solution.** This should be an easy application of A-star search to a grid problem, but I didn't find the time to complete it, because of holiday season obligations."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class HGrid(Grid):\n",
    "    \"\"\"A hashable grid; can be used as a key in dicts.\"\"\"\n",
    "    def __hash__(self): return hash(tuple(sorted(self.items())))\n",
    "    \n",
    "in23 = HGrid(parse(23))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "amphipods  = A, B, C, D = 'ABCD'        # The four kinds of creature\n",
    "amphi_costs= {A:1, B:10, C:100, D:1000} # Cost of each step\n",
    "goal_cols  = {A: 3, B: 5, C: 7, D: 9}   # Column each Amphipod type aspires to\n",
    "hall_row   = 1                          # Row where the hall spaces are\n",
    "side_rows  = {2, 3}                     # Rows every Amphipod aspires to\n",
    "side_rooms = {(goal_cols[L], r) for r in side_rows for L in amphipods}     # All the spaces off of the hallway\n",
    "hallway    = {(c, hall_row) for c in range(1, 12)}                         # Hallway spaces\n",
    "hall_stops = {(c, r) for (c, r) in hallway if c not in goal_cols.values()} # Hallway spaces where you're allowed to stop\n",
    "\n",
    "@dataclass\n",
    "class Move:\n",
    "    start: Point\n",
    "    end: Point\n",
    "    what: Char\n",
    "        \n",
    "def extract_state(grid, kinds):\n",
    "    \"\"\"The parts of the grid of a certain kind.\"\"\"\n",
    "    return {loc: grid[loc] for loc in grid if grid[loc] in kinds}\n",
    "\n",
    "def clear(loc, dest, state):\n",
    "    \"\"\"Is the path from `loc` to `dest` clear of Amphipods in state?\"\"\"\n",
    "    Δx = sign(X_(dest) - X_(loc))\n",
    "    Δy = sign(Y_(dest) - Y_(loc))\n",
    "    return all((X_(loc) + i * Δx, Y_(loc) + i * Δy) not in state\n",
    "               for i in range(1, distance(loc, dest) + 1))\n",
    "\n",
    "def amphi_moves(state) -> Iterable[Move]:\n",
    "    \"\"\"All the moves that can be made from a state.\"\"\"\n",
    "    for loc in state:\n",
    "        # If you're in a side room and you have a clear path to the doorway ...\n",
    "        if loc in side_rooms and clear(loc, (X_(loc), hall_row), state):\n",
    "            # ... then you can move to any hall stop that you have a clear path to\n",
    "            for hall in hall_stops:\n",
    "                if clear((X_(loc), hall_row), hall, state):\n",
    "                    yield Move(loc, hall, state[loc])\n",
    "        # If you're in the hallway and you have a clear path to your doorway ... \n",
    "        goal_col = goal_cols[state[loc]]\n",
    "        if loc in hallway and clear(loc, (goal_col, hall_row), state):\n",
    "            # ... then you can move to the top empty space in your side room if no foreign occupants\n",
    "            occupants = {L for (x, y), L in state.items() if x == goal_col}\n",
    "            if not occupants or occupants == {state[loc]}:\n",
    "                top_row = max(y for y in side_rows if (goal_col, y) not in state)\n",
    "                yield Move(loc, (goal_col, top_row), state[loc])\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Δc = 0\n",
    "Δc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from time import perf_counter as pfc\n",
    "from heapq import heappop, heappush\n",
    "\n",
    "\n",
    "def read_puzzle(filename):\n",
    "  with open(filename) as f:\n",
    "    return ''.join([c for c in f.read() if c in 'ABCD.'])\n",
    "\n",
    "\n",
    "def can_leave_room(puzzle, room_pos):\n",
    "  for a in room_pos:\n",
    "    if puzzle[a] == '.': continue\n",
    "    return a\n",
    "\n",
    "\n",
    "def blocked(a,b,puzzle):\n",
    "  step = 1 if a<b else -1\n",
    "  for pos in range(a+step, b+step, step):\n",
    "    if puzzle[pos] != '.': return True    \n",
    "\n",
    "\n",
    "def get_possible_parc_pos(a,parc,puzzle):\n",
    "  for b in [pos for pos in parc if puzzle[pos] == '.']:\n",
    "    if blocked(a,b,puzzle): continue\n",
    "    yield b\n",
    "\n",
    "\n",
    "def move(a,b,puzzle):\n",
    "  p = list(puzzle)\n",
    "  p[a], p[b] = p[b], p[a]\n",
    "  return ''.join(p)\n",
    "\n",
    "\n",
    "def can_enter_room(a,b,amphi,puzzle,room_pos):\n",
    "  for pos in room_pos:\n",
    "    if puzzle[pos] == '.': best_pos = pos\n",
    "    elif puzzle[pos] != amphi: return False\n",
    "  if not blocked(a,b,puzzle): return best_pos\n",
    "\n",
    "\n",
    "def possible_moves(puzzle, parc, stepout, target):\n",
    "  for a in [pos for pos in parc if puzzle[pos] != '.']:\n",
    "    amphi = puzzle[a]\n",
    "    b=can_enter_room(a, stepout[amphi], amphi, puzzle, target[amphi])\n",
    "    if (b):\n",
    "      yield a,b\n",
    "  for room in 'ABCD':\n",
    "    a=can_leave_room(puzzle, target[room])\n",
    "    if not (a): continue\n",
    "    for b in get_possible_parc_pos(stepout[room], parc, puzzle):\n",
    "      yield a,b\n",
    "\n",
    "\n",
    "def solve(puzzle):\n",
    "  energy = dict(A=1, B=10, C=100, D=1000)\n",
    "  parc= [0,1,3,5,7,9,10]\n",
    "  stepout = dict(A=2, B=4, C=6, D=8)\n",
    "  target = {r: range(ord(r)-54,len(puzzle),4) for r in 'ABCD'}\n",
    "  targetI = {v:key for key,val in target.items() for v in val}\n",
    "\n",
    "  solution = '.'*11+'ABCD'*((len(puzzle)-11)//4)\n",
    "  heap, seen = [(0,puzzle)], {puzzle:0}\n",
    "  while heap:\n",
    "    cost, state = heappop(heap)\n",
    "    if state == solution: return cost\n",
    "    for a,b in possible_moves(state, parc, stepout, target):\n",
    "      p,r = (a,b) if a < b else (b,a)\n",
    "      distance = abs(stepout[targetI[r]] - p) + (r-7)//4\n",
    "      new_cost = cost + distance * energy[state[a]]\n",
    "      moved = move(a,b,state)\n",
    "      if seen.get(moved,999999) <= new_cost: continue\n",
    "      seen[moved] = new_cost\n",
    "      heappush(heap,(new_cost, moved))\n",
    "  \n",
    "\n",
    "  \n",
    "\n",
    "print(solve(read_puzzle(\"AOC/2021/input23.txt\")))\n",
    "#print(solve(read_puzzle(\"Tag_23_b.txt\")))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "extract_from_grid(goal23, '.')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "amphi_goal"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# [Day 24](https://adventofcode.com/2021/day/24): Arithmetic Logic Unit\n",
    "\n",
    "- **Input**: The input is a sequence of assembly language instructions for the submarine's ALU.\n",
    "\n",
    "I'll parse each line into a tuple of atoms:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "in24 = program = parse(24, atoms)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here are the instructions for how to interpret each instruction:\n",
    "\n",
    "- `inp a` - Read an input value and write it to variable a.\n",
    "- `add a b` - Add the value of a to the value of b, then store the result in variable a.\n",
    "- `mul a b` - Multiply the value of a by the value of b, then store the result in variable a.\n",
    "- `div a b` - Divide the value of a by the value of b, truncate the result to an integer, then store the result in variable a. (Here, \"truncate\" means to round the value toward zero.)\n",
    "- `mod a b` - Divide the value of a by the value of b, then store the remainder in variable a. (This is also called the modulo operation.)\n",
    "- `eql a b` - If the value of a and b are equal, then store the value 1 in variable a. Otherwise, store the value 0 in variable a.\n",
    "\n",
    "Note that `b` can be either a variable name or an integer constant. Note there is no branch instruction. Inputs read by the `inp` instruction are one-digit numbers. There are 14 `inp` instructions, and if you feed the program a valid 14-digit model number, it will leave 0 in variable `z`.\n",
    "\n",
    "- **Part 1**: To enable as many submarine features as possible, find the largest valid fourteen-digit model number that contains no 0 digits. **What is the largest model number accepted by MONAD?**\n",
    "\n",
    "Obviously we can't try all 14-digit numbers, so we'll have to somehow understand what the program is doing to find the right model number. But first let's implement the ALU:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def alu(program, inputstr) -> Dict[str, int]:\n",
    "    \"\"\"Run a program with an input string on the submarine's ALU,\n",
    "    and return a dict of the program variables (and {'error': op} if appropriate).\"\"\"\n",
    "    inputs = iter(inputstr)\n",
    "    M = dict(w=0, x=0, y=0, z=0) # Memory\n",
    "    for op, a, *rest in program:\n",
    "        b = M.get(rest[0], rest[0]) if rest else None\n",
    "        if op == 'inp': \n",
    "            M[a] = int(next(inputs))\n",
    "        elif op == 'add': \n",
    "            M[a] += b\n",
    "        elif op == 'mul': \n",
    "            M[a] *= b\n",
    "        elif op == 'div': \n",
    "            if b == 0: return {'error': 1, **M}\n",
    "            M[a] //= b\n",
    "        elif op == 'mod': \n",
    "            if M[a] < 0 or b <= 0: return {'error': 2, **M}\n",
    "            M[a] %= b\n",
    "        elif op == 'eql': \n",
    "            M[a] = int(M[a] == b)\n",
    "    return M\n",
    "        "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can run a program:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "alu(program, '12345678901234')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's figure out where the input instructions are:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "{i for i, ins in enumerate(program) if ins[0] == 'inp'}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "They're every 18 instructions. Let's look at the program divided into 18-instruction segments:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def instr(i) -> str: return f'{i:3d}: ' + ' '.join(map(str, program[i])).ljust(9)\n",
    "\n",
    "for i in range(18):\n",
    "    print(\"| \".join(instr(i + 18 * j) for j in range(6)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Each column is similar, but there are different constants for instructions 4–5 and 15 in each column:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "{i: {instr(i + 18 * j)[5:].strip() for j in range(6)} for i in range(18)}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's try every possible digit as the first input, and see what the program computes right up to the point where it is ready to read the second digit:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "{d: alu(in24[:18], d) for d in '0123456789'}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It looks like after processing one digit, *d*, we have:\n",
    "- w = *d*\n",
    "- x = 1\n",
    "- y = z = *d* + 6\n",
    "    \n",
    "Let's try it for every two-digit input:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def inputs(n) -> List[str]: return [str(i).zfill(n) for i in range(10 ** n)]\n",
    "\n",
    "{i: alu(in24[:36], i) for i in inputs(2)}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "So, *w* is the last digit of the input; *x* is always 1; *y* is 6 more than the last digit, and *z* is 162 + 26 times the first digit plus the last digit.\n",
    "\n",
    "**I was unable to figure out was going on.**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "{i: alu(in24, i) for i in ['10000000000000', \n",
    "                           '12345678901234', \n",
    "                           '11111111111111',\n",
    "                           '23456789876543']}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# [Day 25](): Sea Cucumber\n",
    "\n",
    "- **Input**: The input is a map of east-moving (`>`) and south-moving (`v`) sea cucumbers.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "in25 = Grid(parse(25))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- **Part 1**: Every step, the sea cucumbers in the east-facing herd attempt to move forward one location, then the sea cucumbers in the south-facing herd attempt to move forward one location. When a herd moves forward, every sea cucumber in the herd first simultaneously considers whether there is a sea cucumber in the adjacent location it's facing (even another sea cucumber facing the same direction), and then every sea cucumber facing an empty location simultaneously moves into that location. **Find somewhere safe to land your submarine. What is the first step on which no sea cucumbers move?**\n",
    "\n",
    "Since the herd moves simultaneously, I won't directly update the grid; rather I will keep track of the herd of east-goers (as a set of points) and the herd of south-goers, and update each set on each step. (After extracting the two herds from the grid, it is only used for its width and height)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def move_cukes(grid) -> int:\n",
    "    \"\"\"Move sea cucumbers according to rules until they stop moving.\n",
    "    Return the number of steps it took.\"\"\"\n",
    "    east  = {p for p in grid if grid[p] == '>'}\n",
    "    south = {p for p in grid if grid[p] == 'v'}\n",
    "    for step in count_from(1):\n",
    "        new_east  = set(move_herd(east,  south,    1, 0, grid.width, grid.height))\n",
    "        new_south = set(move_herd(south, new_east, 0, 1, grid.width, grid.height))\n",
    "        if east == new_east and south == new_south:\n",
    "            return step\n",
    "        east, south = new_east, new_south\n",
    "\n",
    "def move_herd(herd: Set[Point], other_herd, dx, dy, width, height) -> Iterator[Point]:\n",
    "    \"\"\"The new positions of the herd; they each move by (dx, dy) if that space is open.\"\"\"\n",
    "    for (x, y) in herd:\n",
    "        p2 = ((x + dx) % width, (y + dy) % height)\n",
    "        yield p2 if (p2 not in herd and p2 not in other_herd) else (x, y)\n",
    "        \n",
    "answer(25.1, 424, lambda: move_cukes(in25))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Unfortunately for me, Part 2 of Day 25 just asked if you had solved all the other puzzles. I hadn't."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Submarine Captain's Log, Supplemental\n",
    "\n",
    "Here is a summary of the results:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "answers"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "I didn't complete days **19, 23, and 24**. 19 is a bit tedious, but shouldn't be conceptually hard. 23 is easy; just need to apply standard graph-search ideas. For 24, I have no idea. I need to reverse engineer the program to understand what it is actually doing. \n",
    "\n",
    "We see that only the following puzzles took more than a second of run time:\n",
    "- **15.2 Chiton**: just over 1 second for search through the 500x500 grid \n",
    "- **20.2 Trench Map**: 4 seconds for enhancing the 100x100 grid for 50 generations\n",
    "- **22.2 Reactor reboot**: 5 seconds for counting a quadrillion cubes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
